git_store.rs

   1pub mod branch_diff;
   2mod conflict_set;
   3pub mod git_traversal;
   4pub mod pending_op;
   5
   6use crate::{
   7    ProjectEnvironment, ProjectItem, ProjectPath,
   8    buffer_store::{BufferStore, BufferStoreEvent},
   9    project_settings::ProjectSettings,
  10    trusted_worktrees::{
  11        PathTrust, TrustedWorktrees, TrustedWorktreesEvent, TrustedWorktreesStore,
  12    },
  13    worktree_store::{WorktreeStore, WorktreeStoreEvent},
  14};
  15use anyhow::{Context as _, Result, anyhow, bail};
  16use askpass::{AskPassDelegate, EncryptedPassword, IKnowWhatIAmDoingAndIHaveReadTheDocs};
  17use buffer_diff::{BufferDiff, BufferDiffEvent};
  18use client::ProjectId;
  19use collections::HashMap;
  20pub use conflict_set::{ConflictRegion, ConflictSet, ConflictSetSnapshot, ConflictSetUpdate};
  21use fs::{Fs, RemoveOptions};
  22use futures::{
  23    FutureExt, StreamExt,
  24    channel::{
  25        mpsc,
  26        oneshot::{self, Canceled},
  27    },
  28    future::{self, BoxFuture, Shared},
  29    stream::FuturesOrdered,
  30};
  31use git::{
  32    BuildPermalinkParams, GitHostingProviderRegistry, Oid, RunHook,
  33    blame::Blame,
  34    parse_git_remote_url,
  35    repository::{
  36        Branch, CommitDetails, CommitDiff, CommitFile, CommitOptions, CreateWorktreeTarget,
  37        DiffType, FetchOptions, GitCommitTemplate, GitRepository, GitRepositoryCheckpoint,
  38        GraphCommitData, InitialGraphCommitData, LogOrder, LogSource, PushOptions, Remote,
  39        RemoteCommandOutput, RepoPath, ResetMode, SearchCommitArgs, UpstreamTrackingStatus,
  40        Worktree as GitWorktree,
  41    },
  42    stash::{GitStash, StashEntry},
  43    status::{
  44        self, DiffStat, DiffTreeType, FileStatus, GitSummary, StatusCode, TrackedStatus, TreeDiff,
  45        TreeDiffStatus, UnmergedStatus, UnmergedStatusCode,
  46    },
  47};
  48use gpui::{
  49    App, AppContext, AsyncApp, Context, Entity, EventEmitter, SharedString, Subscription, Task,
  50    WeakEntity,
  51};
  52use language::{
  53    Buffer, BufferEvent, Language, LanguageRegistry,
  54    proto::{deserialize_version, serialize_version},
  55};
  56use parking_lot::Mutex;
  57use pending_op::{PendingOp, PendingOpId, PendingOps, PendingOpsSummary};
  58use postage::stream::Stream as _;
  59use rpc::{
  60    AnyProtoClient, TypedEnvelope,
  61    proto::{self, git_reset, split_repository_update},
  62};
  63use serde::Deserialize;
  64use settings::{Settings, WorktreeId};
  65use smol::future::yield_now;
  66use std::{
  67    cmp::Ordering,
  68    collections::{BTreeSet, HashSet, VecDeque, hash_map::Entry},
  69    future::Future,
  70    mem,
  71    ops::Range,
  72    path::{Path, PathBuf},
  73    str::FromStr,
  74    sync::{
  75        Arc,
  76        atomic::{self, AtomicU64},
  77    },
  78    time::Instant,
  79};
  80use sum_tree::{Edit, SumTree, TreeMap};
  81use task::Shell;
  82use text::{Bias, BufferId};
  83use util::{
  84    ResultExt, debug_panic,
  85    paths::{PathStyle, SanitizedPath},
  86    post_inc,
  87    rel_path::RelPath,
  88};
  89use worktree::{
  90    File, PathChange, PathKey, PathProgress, PathSummary, PathTarget, ProjectEntryId,
  91    UpdatedGitRepositoriesSet, UpdatedGitRepository, Worktree,
  92};
  93use zeroize::Zeroize;
  94
  95pub struct GitStore {
  96    state: GitStoreState,
  97    buffer_store: Entity<BufferStore>,
  98    worktree_store: Entity<WorktreeStore>,
  99    repositories: HashMap<RepositoryId, Entity<Repository>>,
 100    worktree_ids: HashMap<RepositoryId, HashSet<WorktreeId>>,
 101    active_repo_id: Option<RepositoryId>,
 102    #[allow(clippy::type_complexity)]
 103    loading_diffs:
 104        HashMap<(BufferId, DiffKind), Shared<Task<Result<Entity<BufferDiff>, Arc<anyhow::Error>>>>>,
 105    diffs: HashMap<BufferId, Entity<BufferGitState>>,
 106    shared_diffs: HashMap<proto::PeerId, HashMap<BufferId, SharedDiffs>>,
 107    _subscriptions: Vec<Subscription>,
 108}
 109
 110#[derive(Default)]
 111struct SharedDiffs {
 112    unstaged: Option<Entity<BufferDiff>>,
 113    uncommitted: Option<Entity<BufferDiff>>,
 114}
 115
 116struct BufferGitState {
 117    unstaged_diff: Option<WeakEntity<BufferDiff>>,
 118    uncommitted_diff: Option<WeakEntity<BufferDiff>>,
 119    oid_diffs: HashMap<Option<git::Oid>, WeakEntity<BufferDiff>>,
 120    conflict_set: Option<WeakEntity<ConflictSet>>,
 121    recalculate_diff_task: Option<Task<Result<()>>>,
 122    reparse_conflict_markers_task: Option<Task<Result<()>>>,
 123    language: Option<Arc<Language>>,
 124    language_registry: Option<Arc<LanguageRegistry>>,
 125    conflict_updated_futures: Vec<oneshot::Sender<()>>,
 126    recalculating_tx: postage::watch::Sender<bool>,
 127
 128    /// These operation counts are used to ensure that head and index text
 129    /// values read from the git repository are up-to-date with any hunk staging
 130    /// operations that have been performed on the BufferDiff.
 131    ///
 132    /// The operation count is incremented immediately when the user initiates a
 133    /// hunk stage/unstage operation. Then, upon finishing writing the new index
 134    /// text do disk, the `operation count as of write` is updated to reflect
 135    /// the operation count that prompted the write.
 136    hunk_staging_operation_count: usize,
 137    hunk_staging_operation_count_as_of_write: usize,
 138
 139    head_text: Option<Arc<str>>,
 140    index_text: Option<Arc<str>>,
 141    oid_texts: HashMap<git::Oid, Arc<str>>,
 142    head_changed: bool,
 143    index_changed: bool,
 144    language_changed: bool,
 145}
 146
 147#[derive(Clone, Debug)]
 148enum DiffBasesChange {
 149    SetIndex(Option<String>),
 150    SetHead(Option<String>),
 151    SetEach {
 152        index: Option<String>,
 153        head: Option<String>,
 154    },
 155    SetBoth(Option<String>),
 156}
 157
 158#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
 159enum DiffKind {
 160    Unstaged,
 161    Uncommitted,
 162    SinceOid(Option<git::Oid>),
 163}
 164
 165enum GitStoreState {
 166    Local {
 167        next_repository_id: Arc<AtomicU64>,
 168        downstream: Option<LocalDownstreamState>,
 169        project_environment: Entity<ProjectEnvironment>,
 170        fs: Arc<dyn Fs>,
 171    },
 172    Remote {
 173        upstream_client: AnyProtoClient,
 174        upstream_project_id: u64,
 175        downstream: Option<(AnyProtoClient, ProjectId)>,
 176    },
 177}
 178
 179enum DownstreamUpdate {
 180    UpdateRepository(RepositorySnapshot),
 181    RemoveRepository(RepositoryId),
 182}
 183
 184struct LocalDownstreamState {
 185    client: AnyProtoClient,
 186    project_id: ProjectId,
 187    updates_tx: mpsc::UnboundedSender<DownstreamUpdate>,
 188    _task: Task<Result<()>>,
 189}
 190
 191#[derive(Clone, Debug)]
 192pub struct GitStoreCheckpoint {
 193    checkpoints_by_work_dir_abs_path: HashMap<Arc<Path>, GitRepositoryCheckpoint>,
 194}
 195
 196#[derive(Clone, Debug, PartialEq, Eq)]
 197pub struct StatusEntry {
 198    pub repo_path: RepoPath,
 199    pub status: FileStatus,
 200    pub diff_stat: Option<DiffStat>,
 201}
 202
 203impl StatusEntry {
 204    fn to_proto(&self) -> proto::StatusEntry {
 205        let simple_status = match self.status {
 206            FileStatus::Ignored | FileStatus::Untracked => proto::GitStatus::Added as i32,
 207            FileStatus::Unmerged { .. } => proto::GitStatus::Conflict as i32,
 208            FileStatus::Tracked(TrackedStatus {
 209                index_status,
 210                worktree_status,
 211            }) => tracked_status_to_proto(if worktree_status != StatusCode::Unmodified {
 212                worktree_status
 213            } else {
 214                index_status
 215            }),
 216        };
 217
 218        proto::StatusEntry {
 219            repo_path: self.repo_path.to_proto(),
 220            simple_status,
 221            status: Some(status_to_proto(self.status)),
 222            diff_stat_added: self.diff_stat.map(|ds| ds.added),
 223            diff_stat_deleted: self.diff_stat.map(|ds| ds.deleted),
 224        }
 225    }
 226}
 227
 228impl TryFrom<proto::StatusEntry> for StatusEntry {
 229    type Error = anyhow::Error;
 230
 231    fn try_from(value: proto::StatusEntry) -> Result<Self, Self::Error> {
 232        let repo_path = RepoPath::from_proto(&value.repo_path).context("invalid repo path")?;
 233        let status = status_from_proto(value.simple_status, value.status)?;
 234        let diff_stat = match (value.diff_stat_added, value.diff_stat_deleted) {
 235            (Some(added), Some(deleted)) => Some(DiffStat { added, deleted }),
 236            _ => None,
 237        };
 238        Ok(Self {
 239            repo_path,
 240            status,
 241            diff_stat,
 242        })
 243    }
 244}
 245
 246impl sum_tree::Item for StatusEntry {
 247    type Summary = PathSummary<GitSummary>;
 248
 249    fn summary(&self, _: <Self::Summary as sum_tree::Summary>::Context<'_>) -> Self::Summary {
 250        PathSummary {
 251            max_path: self.repo_path.as_ref().clone(),
 252            item_summary: self.status.summary(),
 253        }
 254    }
 255}
 256
 257impl sum_tree::KeyedItem for StatusEntry {
 258    type Key = PathKey;
 259
 260    fn key(&self) -> Self::Key {
 261        PathKey(self.repo_path.as_ref().clone())
 262    }
 263}
 264
 265#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
 266pub struct RepositoryId(pub u64);
 267
 268#[derive(Clone, Debug, Default, PartialEq, Eq)]
 269pub struct MergeDetails {
 270    pub merge_heads_by_conflicted_path: TreeMap<RepoPath, Vec<Option<SharedString>>>,
 271    pub message: Option<SharedString>,
 272}
 273
 274#[derive(Clone)]
 275pub enum CommitDataState {
 276    Loading,
 277    Loaded(Arc<GraphCommitData>),
 278}
 279
 280#[derive(Clone, Debug, PartialEq, Eq)]
 281pub struct RepositorySnapshot {
 282    pub id: RepositoryId,
 283    pub statuses_by_path: SumTree<StatusEntry>,
 284    pub work_directory_abs_path: Arc<Path>,
 285    /// The working directory of the original repository. For a normal
 286    /// checkout this equals `work_directory_abs_path`. For a git worktree
 287    /// checkout, this is the original repo's working directory — used to
 288    /// anchor new worktree creation so they don't nest.
 289    pub original_repo_abs_path: Arc<Path>,
 290    pub path_style: PathStyle,
 291    pub branch: Option<Branch>,
 292    pub branch_list: Arc<[Branch]>,
 293    pub head_commit: Option<CommitDetails>,
 294    pub scan_id: u64,
 295    pub merge: MergeDetails,
 296    pub remote_origin_url: Option<String>,
 297    pub remote_upstream_url: Option<String>,
 298    pub stash_entries: GitStash,
 299    pub linked_worktrees: Arc<[GitWorktree]>,
 300}
 301
 302type JobId = u64;
 303
 304#[derive(Clone, Debug, PartialEq, Eq)]
 305pub struct JobInfo {
 306    pub start: Instant,
 307    pub message: SharedString,
 308}
 309
 310struct GraphCommitDataHandler {
 311    _task: Task<()>,
 312    commit_data_request: smol::channel::Sender<Oid>,
 313}
 314
 315enum GraphCommitHandlerState {
 316    Starting,
 317    Open(GraphCommitDataHandler),
 318    Closed,
 319}
 320
 321pub struct InitialGitGraphData {
 322    fetch_task: Task<()>,
 323    pub error: Option<SharedString>,
 324    pub commit_data: Vec<Arc<InitialGraphCommitData>>,
 325    pub commit_oid_to_index: HashMap<Oid, usize>,
 326}
 327
 328pub struct GraphDataResponse<'a> {
 329    pub commits: &'a [Arc<InitialGraphCommitData>],
 330    pub is_loading: bool,
 331    pub error: Option<SharedString>,
 332}
 333
 334pub struct Repository {
 335    this: WeakEntity<Self>,
 336    snapshot: RepositorySnapshot,
 337    commit_message_buffer: Option<Entity<Buffer>>,
 338    git_store: WeakEntity<GitStore>,
 339    // For a local repository, holds paths that have had worktree events since the last status scan completed,
 340    // and that should be examined during the next status scan.
 341    paths_needing_status_update: Vec<Vec<RepoPath>>,
 342    job_sender: mpsc::UnboundedSender<GitJob>,
 343    active_jobs: HashMap<JobId, JobInfo>,
 344    pending_ops: SumTree<PendingOps>,
 345    job_id: JobId,
 346    askpass_delegates: Arc<Mutex<HashMap<u64, AskPassDelegate>>>,
 347    latest_askpass_id: u64,
 348    repository_state: Shared<Task<Result<RepositoryState, String>>>,
 349    initial_graph_data: HashMap<(LogSource, LogOrder), InitialGitGraphData>,
 350    graph_commit_data_handler: GraphCommitHandlerState,
 351    commit_data: HashMap<Oid, CommitDataState>,
 352}
 353
 354impl std::ops::Deref for Repository {
 355    type Target = RepositorySnapshot;
 356
 357    fn deref(&self) -> &Self::Target {
 358        &self.snapshot
 359    }
 360}
 361
 362#[derive(Clone)]
 363pub struct LocalRepositoryState {
 364    pub fs: Arc<dyn Fs>,
 365    pub backend: Arc<dyn GitRepository>,
 366    pub environment: Arc<HashMap<String, String>>,
 367}
 368
 369impl LocalRepositoryState {
 370    async fn new(
 371        work_directory_abs_path: Arc<Path>,
 372        dot_git_abs_path: Arc<Path>,
 373        project_environment: WeakEntity<ProjectEnvironment>,
 374        fs: Arc<dyn Fs>,
 375        is_trusted: bool,
 376        cx: &mut AsyncApp,
 377    ) -> anyhow::Result<Self> {
 378        let environment = project_environment
 379                .update(cx, |project_environment, cx| {
 380                    project_environment.local_directory_environment(&Shell::System, work_directory_abs_path.clone(), cx)
 381                })?
 382                .await
 383                .unwrap_or_else(|| {
 384                    log::error!("failed to get working directory environment for repository {work_directory_abs_path:?}");
 385                    HashMap::default()
 386                });
 387        let search_paths = environment.get("PATH").map(|val| val.to_owned());
 388        let backend = cx
 389            .background_spawn({
 390                let fs = fs.clone();
 391                async move {
 392                    let system_git_binary_path = search_paths
 393                        .and_then(|search_paths| {
 394                            which::which_in("git", Some(search_paths), &work_directory_abs_path)
 395                                .ok()
 396                        })
 397                        .or_else(|| which::which("git").ok());
 398                    fs.open_repo(&dot_git_abs_path, system_git_binary_path.as_deref())
 399                        .with_context(|| format!("opening repository at {dot_git_abs_path:?}"))
 400                }
 401            })
 402            .await?;
 403        backend.set_trusted(is_trusted);
 404        Ok(LocalRepositoryState {
 405            backend,
 406            environment: Arc::new(environment),
 407            fs,
 408        })
 409    }
 410}
 411
 412#[derive(Clone)]
 413pub struct RemoteRepositoryState {
 414    pub project_id: ProjectId,
 415    pub client: AnyProtoClient,
 416}
 417
 418#[derive(Clone)]
 419pub enum RepositoryState {
 420    Local(LocalRepositoryState),
 421    Remote(RemoteRepositoryState),
 422}
 423
 424#[derive(Clone, Debug, PartialEq, Eq)]
 425pub enum GitGraphEvent {
 426    CountUpdated(usize),
 427    FullyLoaded,
 428    LoadingError,
 429}
 430
 431#[derive(Clone, Debug, PartialEq, Eq)]
 432pub enum RepositoryEvent {
 433    StatusesChanged,
 434    HeadChanged,
 435    BranchListChanged,
 436    StashEntriesChanged,
 437    GitWorktreeListChanged,
 438    PendingOpsChanged { pending_ops: SumTree<PendingOps> },
 439    GraphEvent((LogSource, LogOrder), GitGraphEvent),
 440}
 441
 442#[derive(Clone, Debug)]
 443pub struct JobsUpdated;
 444
 445#[derive(Debug)]
 446pub enum GitStoreEvent {
 447    ActiveRepositoryChanged(Option<RepositoryId>),
 448    /// Bool is true when the repository that's updated is the active repository
 449    RepositoryUpdated(RepositoryId, RepositoryEvent, bool),
 450    RepositoryAdded,
 451    RepositoryRemoved(RepositoryId),
 452    IndexWriteError(anyhow::Error),
 453    JobsUpdated,
 454    ConflictsUpdated,
 455}
 456
 457impl EventEmitter<RepositoryEvent> for Repository {}
 458impl EventEmitter<JobsUpdated> for Repository {}
 459impl EventEmitter<GitStoreEvent> for GitStore {}
 460
 461pub struct GitJob {
 462    job: Box<dyn FnOnce(RepositoryState, &mut AsyncApp) -> Task<()>>,
 463    key: Option<GitJobKey>,
 464}
 465
 466#[derive(PartialEq, Eq)]
 467enum GitJobKey {
 468    WriteIndex(Vec<RepoPath>),
 469    ReloadBufferDiffBases,
 470    RefreshStatuses,
 471    ReloadGitState,
 472}
 473
 474impl GitStore {
 475    pub fn local(
 476        worktree_store: &Entity<WorktreeStore>,
 477        buffer_store: Entity<BufferStore>,
 478        environment: Entity<ProjectEnvironment>,
 479        fs: Arc<dyn Fs>,
 480        cx: &mut Context<Self>,
 481    ) -> Self {
 482        Self::new(
 483            worktree_store.clone(),
 484            buffer_store,
 485            GitStoreState::Local {
 486                next_repository_id: Arc::new(AtomicU64::new(1)),
 487                downstream: None,
 488                project_environment: environment,
 489                fs,
 490            },
 491            cx,
 492        )
 493    }
 494
 495    pub fn remote(
 496        worktree_store: &Entity<WorktreeStore>,
 497        buffer_store: Entity<BufferStore>,
 498        upstream_client: AnyProtoClient,
 499        project_id: u64,
 500        cx: &mut Context<Self>,
 501    ) -> Self {
 502        Self::new(
 503            worktree_store.clone(),
 504            buffer_store,
 505            GitStoreState::Remote {
 506                upstream_client,
 507                upstream_project_id: project_id,
 508                downstream: None,
 509            },
 510            cx,
 511        )
 512    }
 513
 514    fn new(
 515        worktree_store: Entity<WorktreeStore>,
 516        buffer_store: Entity<BufferStore>,
 517        state: GitStoreState,
 518        cx: &mut Context<Self>,
 519    ) -> Self {
 520        let mut _subscriptions = vec![
 521            cx.subscribe(&worktree_store, Self::on_worktree_store_event),
 522            cx.subscribe(&buffer_store, Self::on_buffer_store_event),
 523        ];
 524
 525        if let Some(trusted_worktrees) = TrustedWorktrees::try_get_global(cx) {
 526            _subscriptions.push(cx.subscribe(&trusted_worktrees, Self::on_trusted_worktrees_event));
 527        }
 528
 529        GitStore {
 530            state,
 531            buffer_store,
 532            worktree_store,
 533            repositories: HashMap::default(),
 534            worktree_ids: HashMap::default(),
 535            active_repo_id: None,
 536            _subscriptions,
 537            loading_diffs: HashMap::default(),
 538            shared_diffs: HashMap::default(),
 539            diffs: HashMap::default(),
 540        }
 541    }
 542
 543    pub fn init(client: &AnyProtoClient) {
 544        client.add_entity_request_handler(Self::handle_get_remotes);
 545        client.add_entity_request_handler(Self::handle_get_branches);
 546        client.add_entity_request_handler(Self::handle_get_default_branch);
 547        client.add_entity_request_handler(Self::handle_change_branch);
 548        client.add_entity_request_handler(Self::handle_create_branch);
 549        client.add_entity_request_handler(Self::handle_rename_branch);
 550        client.add_entity_request_handler(Self::handle_create_remote);
 551        client.add_entity_request_handler(Self::handle_remove_remote);
 552        client.add_entity_request_handler(Self::handle_delete_branch);
 553        client.add_entity_request_handler(Self::handle_git_init);
 554        client.add_entity_request_handler(Self::handle_push);
 555        client.add_entity_request_handler(Self::handle_pull);
 556        client.add_entity_request_handler(Self::handle_fetch);
 557        client.add_entity_request_handler(Self::handle_stage);
 558        client.add_entity_request_handler(Self::handle_unstage);
 559        client.add_entity_request_handler(Self::handle_stash);
 560        client.add_entity_request_handler(Self::handle_stash_pop);
 561        client.add_entity_request_handler(Self::handle_stash_apply);
 562        client.add_entity_request_handler(Self::handle_stash_drop);
 563        client.add_entity_request_handler(Self::handle_commit);
 564        client.add_entity_request_handler(Self::handle_run_hook);
 565        client.add_entity_request_handler(Self::handle_reset);
 566        client.add_entity_request_handler(Self::handle_show);
 567        client.add_entity_request_handler(Self::handle_create_checkpoint);
 568        client.add_entity_request_handler(Self::handle_create_archive_checkpoint);
 569        client.add_entity_request_handler(Self::handle_restore_checkpoint);
 570        client.add_entity_request_handler(Self::handle_restore_archive_checkpoint);
 571        client.add_entity_request_handler(Self::handle_compare_checkpoints);
 572        client.add_entity_request_handler(Self::handle_diff_checkpoints);
 573        client.add_entity_request_handler(Self::handle_load_commit_diff);
 574        client.add_entity_request_handler(Self::handle_file_history);
 575        client.add_entity_request_handler(Self::handle_checkout_files);
 576        client.add_entity_request_handler(Self::handle_open_commit_message_buffer);
 577        client.add_entity_request_handler(Self::handle_set_index_text);
 578        client.add_entity_request_handler(Self::handle_askpass);
 579        client.add_entity_request_handler(Self::handle_check_for_pushed_commits);
 580        client.add_entity_request_handler(Self::handle_git_diff);
 581        client.add_entity_request_handler(Self::handle_tree_diff);
 582        client.add_entity_request_handler(Self::handle_get_blob_content);
 583        client.add_entity_request_handler(Self::handle_open_unstaged_diff);
 584        client.add_entity_request_handler(Self::handle_open_uncommitted_diff);
 585        client.add_entity_message_handler(Self::handle_update_diff_bases);
 586        client.add_entity_request_handler(Self::handle_get_permalink_to_line);
 587        client.add_entity_request_handler(Self::handle_blame_buffer);
 588        client.add_entity_message_handler(Self::handle_update_repository);
 589        client.add_entity_message_handler(Self::handle_remove_repository);
 590        client.add_entity_request_handler(Self::handle_git_clone);
 591        client.add_entity_request_handler(Self::handle_get_worktrees);
 592        client.add_entity_request_handler(Self::handle_create_worktree);
 593        client.add_entity_request_handler(Self::handle_remove_worktree);
 594        client.add_entity_request_handler(Self::handle_rename_worktree);
 595        client.add_entity_request_handler(Self::handle_get_head_sha);
 596        client.add_entity_request_handler(Self::handle_edit_ref);
 597        client.add_entity_request_handler(Self::handle_repair_worktrees);
 598    }
 599
 600    pub fn is_local(&self) -> bool {
 601        matches!(self.state, GitStoreState::Local { .. })
 602    }
 603
 604    fn set_active_repo_id(&mut self, repo_id: RepositoryId, cx: &mut Context<Self>) {
 605        if self.active_repo_id != Some(repo_id) {
 606            self.active_repo_id = Some(repo_id);
 607            cx.emit(GitStoreEvent::ActiveRepositoryChanged(Some(repo_id)));
 608        }
 609    }
 610
 611    pub fn set_active_repo_for_path(&mut self, project_path: &ProjectPath, cx: &mut Context<Self>) {
 612        if let Some((repo, _)) = self.repository_and_path_for_project_path(project_path, cx) {
 613            self.set_active_repo_id(repo.read(cx).id, cx);
 614        }
 615    }
 616
 617    pub fn set_active_repo_for_worktree(
 618        &mut self,
 619        worktree_id: WorktreeId,
 620        cx: &mut Context<Self>,
 621    ) {
 622        let Some(worktree) = self
 623            .worktree_store
 624            .read(cx)
 625            .worktree_for_id(worktree_id, cx)
 626        else {
 627            return;
 628        };
 629        let worktree_abs_path = worktree.read(cx).abs_path();
 630        let Some(repo_id) = self
 631            .repositories
 632            .values()
 633            .filter(|repo| {
 634                let repo_path = &repo.read(cx).work_directory_abs_path;
 635                *repo_path == worktree_abs_path || worktree_abs_path.starts_with(repo_path.as_ref())
 636            })
 637            .max_by_key(|repo| repo.read(cx).work_directory_abs_path.as_os_str().len())
 638            .map(|repo| repo.read(cx).id)
 639        else {
 640            return;
 641        };
 642
 643        self.set_active_repo_id(repo_id, cx);
 644    }
 645
 646    pub fn shared(&mut self, project_id: u64, client: AnyProtoClient, cx: &mut Context<Self>) {
 647        match &mut self.state {
 648            GitStoreState::Remote {
 649                downstream: downstream_client,
 650                ..
 651            } => {
 652                for repo in self.repositories.values() {
 653                    let update = repo.read(cx).snapshot.initial_update(project_id);
 654                    for update in split_repository_update(update) {
 655                        client.send(update).log_err();
 656                    }
 657                }
 658                *downstream_client = Some((client, ProjectId(project_id)));
 659            }
 660            GitStoreState::Local {
 661                downstream: downstream_client,
 662                ..
 663            } => {
 664                let mut snapshots = HashMap::default();
 665                let (updates_tx, mut updates_rx) = mpsc::unbounded();
 666                for repo in self.repositories.values() {
 667                    updates_tx
 668                        .unbounded_send(DownstreamUpdate::UpdateRepository(
 669                            repo.read(cx).snapshot.clone(),
 670                        ))
 671                        .ok();
 672                }
 673                *downstream_client = Some(LocalDownstreamState {
 674                    client: client.clone(),
 675                    project_id: ProjectId(project_id),
 676                    updates_tx,
 677                    _task: cx.spawn(async move |this, cx| {
 678                        cx.background_spawn(async move {
 679                            while let Some(update) = updates_rx.next().await {
 680                                match update {
 681                                    DownstreamUpdate::UpdateRepository(snapshot) => {
 682                                        if let Some(old_snapshot) = snapshots.get_mut(&snapshot.id)
 683                                        {
 684                                            let update =
 685                                                snapshot.build_update(old_snapshot, project_id);
 686                                            *old_snapshot = snapshot;
 687                                            for update in split_repository_update(update) {
 688                                                client.send(update)?;
 689                                            }
 690                                        } else {
 691                                            let update = snapshot.initial_update(project_id);
 692                                            for update in split_repository_update(update) {
 693                                                client.send(update)?;
 694                                            }
 695                                            snapshots.insert(snapshot.id, snapshot);
 696                                        }
 697                                    }
 698                                    DownstreamUpdate::RemoveRepository(id) => {
 699                                        client.send(proto::RemoveRepository {
 700                                            project_id,
 701                                            id: id.to_proto(),
 702                                        })?;
 703                                    }
 704                                }
 705                            }
 706                            anyhow::Ok(())
 707                        })
 708                        .await
 709                        .ok();
 710                        this.update(cx, |this, _| {
 711                            if let GitStoreState::Local {
 712                                downstream: downstream_client,
 713                                ..
 714                            } = &mut this.state
 715                            {
 716                                downstream_client.take();
 717                            } else {
 718                                unreachable!("unshared called on remote store");
 719                            }
 720                        })
 721                    }),
 722                });
 723            }
 724        }
 725    }
 726
 727    pub fn unshared(&mut self, _cx: &mut Context<Self>) {
 728        match &mut self.state {
 729            GitStoreState::Local {
 730                downstream: downstream_client,
 731                ..
 732            } => {
 733                downstream_client.take();
 734            }
 735            GitStoreState::Remote {
 736                downstream: downstream_client,
 737                ..
 738            } => {
 739                downstream_client.take();
 740            }
 741        }
 742        self.shared_diffs.clear();
 743    }
 744
 745    pub(crate) fn forget_shared_diffs_for(&mut self, peer_id: &proto::PeerId) {
 746        self.shared_diffs.remove(peer_id);
 747    }
 748
 749    pub fn active_repository(&self) -> Option<Entity<Repository>> {
 750        self.active_repo_id
 751            .as_ref()
 752            .map(|id| self.repositories[id].clone())
 753    }
 754
 755    pub fn open_unstaged_diff(
 756        &mut self,
 757        buffer: Entity<Buffer>,
 758        cx: &mut Context<Self>,
 759    ) -> Task<Result<Entity<BufferDiff>>> {
 760        let buffer_id = buffer.read(cx).remote_id();
 761        if let Some(diff_state) = self.diffs.get(&buffer_id)
 762            && let Some(unstaged_diff) = diff_state
 763                .read(cx)
 764                .unstaged_diff
 765                .as_ref()
 766                .and_then(|weak| weak.upgrade())
 767        {
 768            if let Some(task) =
 769                diff_state.update(cx, |diff_state, _| diff_state.wait_for_recalculation())
 770            {
 771                return cx.background_executor().spawn(async move {
 772                    task.await;
 773                    Ok(unstaged_diff)
 774                });
 775            }
 776            return Task::ready(Ok(unstaged_diff));
 777        }
 778
 779        let Some((repo, repo_path)) =
 780            self.repository_and_path_for_buffer_id(buffer.read(cx).remote_id(), cx)
 781        else {
 782            return Task::ready(Err(anyhow!("failed to find git repository for buffer")));
 783        };
 784
 785        let task = self
 786            .loading_diffs
 787            .entry((buffer_id, DiffKind::Unstaged))
 788            .or_insert_with(|| {
 789                let staged_text = repo.update(cx, |repo, cx| {
 790                    repo.load_staged_text(buffer_id, repo_path, cx)
 791                });
 792                cx.spawn(async move |this, cx| {
 793                    Self::open_diff_internal(
 794                        this,
 795                        DiffKind::Unstaged,
 796                        staged_text.await.map(DiffBasesChange::SetIndex),
 797                        buffer,
 798                        cx,
 799                    )
 800                    .await
 801                    .map_err(Arc::new)
 802                })
 803                .shared()
 804            })
 805            .clone();
 806
 807        cx.background_spawn(async move { task.await.map_err(|e| anyhow!("{e}")) })
 808    }
 809
 810    pub fn open_diff_since(
 811        &mut self,
 812        oid: Option<git::Oid>,
 813        buffer: Entity<Buffer>,
 814        repo: Entity<Repository>,
 815        cx: &mut Context<Self>,
 816    ) -> Task<Result<Entity<BufferDiff>>> {
 817        let buffer_id = buffer.read(cx).remote_id();
 818
 819        if let Some(diff_state) = self.diffs.get(&buffer_id)
 820            && let Some(oid_diff) = diff_state.read(cx).oid_diff(oid)
 821        {
 822            if let Some(task) =
 823                diff_state.update(cx, |diff_state, _| diff_state.wait_for_recalculation())
 824            {
 825                return cx.background_executor().spawn(async move {
 826                    task.await;
 827                    Ok(oid_diff)
 828                });
 829            }
 830            return Task::ready(Ok(oid_diff));
 831        }
 832
 833        let diff_kind = DiffKind::SinceOid(oid);
 834        if let Some(task) = self.loading_diffs.get(&(buffer_id, diff_kind)) {
 835            let task = task.clone();
 836            return cx.background_spawn(async move { task.await.map_err(|e| anyhow!("{e}")) });
 837        }
 838
 839        let task = cx
 840            .spawn(async move |this, cx| {
 841                let result: Result<Entity<BufferDiff>> = async {
 842                    let buffer_snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());
 843                    let language_registry =
 844                        buffer.update(cx, |buffer, _| buffer.language_registry());
 845                    let content: Option<Arc<str>> = match oid {
 846                        None => None,
 847                        Some(oid) => Some(
 848                            repo.update(cx, |repo, cx| repo.load_blob_content(oid, cx))
 849                                .await?
 850                                .into(),
 851                        ),
 852                    };
 853                    let buffer_diff = cx.new(|cx| BufferDiff::new(&buffer_snapshot, cx));
 854
 855                    buffer_diff
 856                        .update(cx, |buffer_diff, cx| {
 857                            buffer_diff.language_changed(
 858                                buffer_snapshot.language().cloned(),
 859                                language_registry,
 860                                cx,
 861                            );
 862                            buffer_diff.set_base_text(
 863                                content.clone(),
 864                                buffer_snapshot.language().cloned(),
 865                                buffer_snapshot.text,
 866                                cx,
 867                            )
 868                        })
 869                        .await?;
 870                    let unstaged_diff = this
 871                        .update(cx, |this, cx| this.open_unstaged_diff(buffer.clone(), cx))?
 872                        .await?;
 873                    buffer_diff.update(cx, |buffer_diff, _| {
 874                        buffer_diff.set_secondary_diff(unstaged_diff);
 875                    });
 876
 877                    this.update(cx, |this, cx| {
 878                        cx.subscribe(&buffer_diff, Self::on_buffer_diff_event)
 879                            .detach();
 880
 881                        this.loading_diffs.remove(&(buffer_id, diff_kind));
 882
 883                        let git_store = cx.weak_entity();
 884                        let diff_state = this
 885                            .diffs
 886                            .entry(buffer_id)
 887                            .or_insert_with(|| cx.new(|_| BufferGitState::new(git_store)));
 888
 889                        diff_state.update(cx, |state, _| {
 890                            if let Some(oid) = oid {
 891                                if let Some(content) = content {
 892                                    state.oid_texts.insert(oid, content);
 893                                }
 894                            }
 895                            state.oid_diffs.insert(oid, buffer_diff.downgrade());
 896                        });
 897                    })?;
 898
 899                    Ok(buffer_diff)
 900                }
 901                .await;
 902                result.map_err(Arc::new)
 903            })
 904            .shared();
 905
 906        self.loading_diffs
 907            .insert((buffer_id, diff_kind), task.clone());
 908        cx.background_spawn(async move { task.await.map_err(|e| anyhow!("{e}")) })
 909    }
 910
 911    #[ztracing::instrument(skip_all)]
 912    pub fn open_uncommitted_diff(
 913        &mut self,
 914        buffer: Entity<Buffer>,
 915        cx: &mut Context<Self>,
 916    ) -> Task<Result<Entity<BufferDiff>>> {
 917        let buffer_id = buffer.read(cx).remote_id();
 918
 919        if let Some(diff_state) = self.diffs.get(&buffer_id)
 920            && let Some(uncommitted_diff) = diff_state
 921                .read(cx)
 922                .uncommitted_diff
 923                .as_ref()
 924                .and_then(|weak| weak.upgrade())
 925        {
 926            if let Some(task) =
 927                diff_state.update(cx, |diff_state, _| diff_state.wait_for_recalculation())
 928            {
 929                return cx.background_executor().spawn(async move {
 930                    task.await;
 931                    Ok(uncommitted_diff)
 932                });
 933            }
 934            return Task::ready(Ok(uncommitted_diff));
 935        }
 936
 937        let Some((repo, repo_path)) =
 938            self.repository_and_path_for_buffer_id(buffer.read(cx).remote_id(), cx)
 939        else {
 940            return Task::ready(Err(anyhow!("failed to find git repository for buffer")));
 941        };
 942
 943        let task = self
 944            .loading_diffs
 945            .entry((buffer_id, DiffKind::Uncommitted))
 946            .or_insert_with(|| {
 947                let changes = repo.update(cx, |repo, cx| {
 948                    repo.load_committed_text(buffer_id, repo_path, cx)
 949                });
 950
 951                // todo(lw): hot foreground spawn
 952                cx.spawn(async move |this, cx| {
 953                    Self::open_diff_internal(this, DiffKind::Uncommitted, changes.await, buffer, cx)
 954                        .await
 955                        .map_err(Arc::new)
 956                })
 957                .shared()
 958            })
 959            .clone();
 960
 961        cx.background_spawn(async move { task.await.map_err(|e| anyhow!("{e}")) })
 962    }
 963
 964    #[ztracing::instrument(skip_all)]
 965    async fn open_diff_internal(
 966        this: WeakEntity<Self>,
 967        kind: DiffKind,
 968        texts: Result<DiffBasesChange>,
 969        buffer_entity: Entity<Buffer>,
 970        cx: &mut AsyncApp,
 971    ) -> Result<Entity<BufferDiff>> {
 972        let diff_bases_change = match texts {
 973            Err(e) => {
 974                this.update(cx, |this, cx| {
 975                    let buffer = buffer_entity.read(cx);
 976                    let buffer_id = buffer.remote_id();
 977                    this.loading_diffs.remove(&(buffer_id, kind));
 978                })?;
 979                return Err(e);
 980            }
 981            Ok(change) => change,
 982        };
 983
 984        this.update(cx, |this, cx| {
 985            let buffer = buffer_entity.read(cx);
 986            let buffer_id = buffer.remote_id();
 987            let language = buffer.language().cloned();
 988            let language_registry = buffer.language_registry();
 989            let text_snapshot = buffer.text_snapshot();
 990            this.loading_diffs.remove(&(buffer_id, kind));
 991
 992            let git_store = cx.weak_entity();
 993            let diff_state = this
 994                .diffs
 995                .entry(buffer_id)
 996                .or_insert_with(|| cx.new(|_| BufferGitState::new(git_store)));
 997
 998            let diff = cx.new(|cx| BufferDiff::new(&text_snapshot, cx));
 999
1000            cx.subscribe(&diff, Self::on_buffer_diff_event).detach();
1001            diff_state.update(cx, |diff_state, cx| {
1002                diff_state.language_changed = true;
1003                diff_state.language = language;
1004                diff_state.language_registry = language_registry;
1005
1006                match kind {
1007                    DiffKind::Unstaged => {
1008                        diff_state.unstaged_diff.get_or_insert(diff.downgrade());
1009                    }
1010                    DiffKind::Uncommitted => {
1011                        let unstaged_diff = if let Some(diff) = diff_state.unstaged_diff() {
1012                            diff
1013                        } else {
1014                            let unstaged_diff = cx.new(|cx| BufferDiff::new(&text_snapshot, cx));
1015                            diff_state.unstaged_diff = Some(unstaged_diff.downgrade());
1016                            unstaged_diff
1017                        };
1018
1019                        diff.update(cx, |diff, _| diff.set_secondary_diff(unstaged_diff));
1020                        diff_state.uncommitted_diff = Some(diff.downgrade())
1021                    }
1022                    DiffKind::SinceOid(_) => {
1023                        unreachable!("open_diff_internal is not used for OID diffs")
1024                    }
1025                }
1026
1027                diff_state.diff_bases_changed(text_snapshot, Some(diff_bases_change), cx);
1028                let rx = diff_state.wait_for_recalculation();
1029
1030                anyhow::Ok(async move {
1031                    if let Some(rx) = rx {
1032                        rx.await;
1033                    }
1034                    Ok(diff)
1035                })
1036            })
1037        })??
1038        .await
1039    }
1040
1041    pub fn get_unstaged_diff(&self, buffer_id: BufferId, cx: &App) -> Option<Entity<BufferDiff>> {
1042        let diff_state = self.diffs.get(&buffer_id)?;
1043        diff_state.read(cx).unstaged_diff.as_ref()?.upgrade()
1044    }
1045
1046    pub fn get_uncommitted_diff(
1047        &self,
1048        buffer_id: BufferId,
1049        cx: &App,
1050    ) -> Option<Entity<BufferDiff>> {
1051        let diff_state = self.diffs.get(&buffer_id)?;
1052        diff_state.read(cx).uncommitted_diff.as_ref()?.upgrade()
1053    }
1054
1055    pub fn get_diff_since_oid(
1056        &self,
1057        buffer_id: BufferId,
1058        oid: Option<git::Oid>,
1059        cx: &App,
1060    ) -> Option<Entity<BufferDiff>> {
1061        let diff_state = self.diffs.get(&buffer_id)?;
1062        diff_state.read(cx).oid_diff(oid)
1063    }
1064
1065    pub fn open_conflict_set(
1066        &mut self,
1067        buffer: Entity<Buffer>,
1068        cx: &mut Context<Self>,
1069    ) -> Entity<ConflictSet> {
1070        log::debug!("open conflict set");
1071        let buffer_id = buffer.read(cx).remote_id();
1072
1073        if let Some(git_state) = self.diffs.get(&buffer_id)
1074            && let Some(conflict_set) = git_state
1075                .read(cx)
1076                .conflict_set
1077                .as_ref()
1078                .and_then(|weak| weak.upgrade())
1079        {
1080            let conflict_set = conflict_set;
1081            let buffer_snapshot = buffer.read(cx).text_snapshot();
1082
1083            git_state.update(cx, |state, cx| {
1084                let _ = state.reparse_conflict_markers(buffer_snapshot, cx);
1085            });
1086
1087            return conflict_set;
1088        }
1089
1090        let is_unmerged = self
1091            .repository_and_path_for_buffer_id(buffer_id, cx)
1092            .is_some_and(|(repo, path)| repo.read(cx).snapshot.has_conflict(&path));
1093        let git_store = cx.weak_entity();
1094        let buffer_git_state = self
1095            .diffs
1096            .entry(buffer_id)
1097            .or_insert_with(|| cx.new(|_| BufferGitState::new(git_store)));
1098        let conflict_set = cx.new(|cx| ConflictSet::new(buffer_id, is_unmerged, cx));
1099
1100        self._subscriptions
1101            .push(cx.subscribe(&conflict_set, |_, _, _, cx| {
1102                cx.emit(GitStoreEvent::ConflictsUpdated);
1103            }));
1104
1105        buffer_git_state.update(cx, |state, cx| {
1106            state.conflict_set = Some(conflict_set.downgrade());
1107            let buffer_snapshot = buffer.read(cx).text_snapshot();
1108            let _ = state.reparse_conflict_markers(buffer_snapshot, cx);
1109        });
1110
1111        conflict_set
1112    }
1113
1114    pub fn project_path_git_status(
1115        &self,
1116        project_path: &ProjectPath,
1117        cx: &App,
1118    ) -> Option<FileStatus> {
1119        let (repo, repo_path) = self.repository_and_path_for_project_path(project_path, cx)?;
1120        Some(repo.read(cx).status_for_path(&repo_path)?.status)
1121    }
1122
1123    pub fn checkpoint(&self, cx: &mut App) -> Task<Result<GitStoreCheckpoint>> {
1124        let mut work_directory_abs_paths = Vec::new();
1125        let mut checkpoints = Vec::new();
1126        for repository in self.repositories.values() {
1127            repository.update(cx, |repository, _| {
1128                work_directory_abs_paths.push(repository.snapshot.work_directory_abs_path.clone());
1129                checkpoints.push(repository.checkpoint().map(|checkpoint| checkpoint?));
1130            });
1131        }
1132
1133        cx.background_executor().spawn(async move {
1134            let checkpoints = future::try_join_all(checkpoints).await?;
1135            Ok(GitStoreCheckpoint {
1136                checkpoints_by_work_dir_abs_path: work_directory_abs_paths
1137                    .into_iter()
1138                    .zip(checkpoints)
1139                    .collect(),
1140            })
1141        })
1142    }
1143
1144    pub fn restore_checkpoint(
1145        &self,
1146        checkpoint: GitStoreCheckpoint,
1147        cx: &mut App,
1148    ) -> Task<Result<()>> {
1149        let repositories_by_work_dir_abs_path = self
1150            .repositories
1151            .values()
1152            .map(|repo| (repo.read(cx).snapshot.work_directory_abs_path.clone(), repo))
1153            .collect::<HashMap<_, _>>();
1154
1155        let mut tasks = Vec::new();
1156        for (work_dir_abs_path, checkpoint) in checkpoint.checkpoints_by_work_dir_abs_path {
1157            if let Some(repository) = repositories_by_work_dir_abs_path.get(&work_dir_abs_path) {
1158                let restore = repository.update(cx, |repository, _| {
1159                    repository.restore_checkpoint(checkpoint)
1160                });
1161                tasks.push(async move { restore.await? });
1162            }
1163        }
1164        cx.background_spawn(async move {
1165            future::try_join_all(tasks).await?;
1166            Ok(())
1167        })
1168    }
1169
1170    /// Compares two checkpoints, returning true if they are equal.
1171    pub fn compare_checkpoints(
1172        &self,
1173        left: GitStoreCheckpoint,
1174        mut right: GitStoreCheckpoint,
1175        cx: &mut App,
1176    ) -> Task<Result<bool>> {
1177        let repositories_by_work_dir_abs_path = self
1178            .repositories
1179            .values()
1180            .map(|repo| (repo.read(cx).snapshot.work_directory_abs_path.clone(), repo))
1181            .collect::<HashMap<_, _>>();
1182
1183        let mut tasks = Vec::new();
1184        for (work_dir_abs_path, left_checkpoint) in left.checkpoints_by_work_dir_abs_path {
1185            if let Some(right_checkpoint) = right
1186                .checkpoints_by_work_dir_abs_path
1187                .remove(&work_dir_abs_path)
1188            {
1189                if let Some(repository) = repositories_by_work_dir_abs_path.get(&work_dir_abs_path)
1190                {
1191                    let compare = repository.update(cx, |repository, _| {
1192                        repository.compare_checkpoints(left_checkpoint, right_checkpoint)
1193                    });
1194
1195                    tasks.push(async move { compare.await? });
1196                }
1197            } else {
1198                return Task::ready(Ok(false));
1199            }
1200        }
1201        cx.background_spawn(async move {
1202            Ok(future::try_join_all(tasks)
1203                .await?
1204                .into_iter()
1205                .all(|result| result))
1206        })
1207    }
1208
1209    /// Blames a buffer.
1210    pub fn blame_buffer(
1211        &self,
1212        buffer: &Entity<Buffer>,
1213        version: Option<clock::Global>,
1214        cx: &mut Context<Self>,
1215    ) -> Task<Result<Option<Blame>>> {
1216        let buffer = buffer.read(cx);
1217        let Some((repo, repo_path)) =
1218            self.repository_and_path_for_buffer_id(buffer.remote_id(), cx)
1219        else {
1220            return Task::ready(Err(anyhow!("failed to find a git repository for buffer")));
1221        };
1222        let content = match &version {
1223            Some(version) => buffer.rope_for_version(version),
1224            None => buffer.as_rope().clone(),
1225        };
1226        let line_ending = buffer.line_ending();
1227        let version = version.unwrap_or(buffer.version());
1228        let buffer_id = buffer.remote_id();
1229
1230        let repo = repo.downgrade();
1231        cx.spawn(async move |_, cx| {
1232            let repository_state = repo
1233                .update(cx, |repo, _| repo.repository_state.clone())?
1234                .await
1235                .map_err(|err| anyhow::anyhow!(err))?;
1236            match repository_state {
1237                RepositoryState::Local(LocalRepositoryState { backend, .. }) => backend
1238                    .blame(repo_path.clone(), content, line_ending)
1239                    .await
1240                    .with_context(|| format!("Failed to blame {:?}", repo_path.as_ref()))
1241                    .map(Some),
1242                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
1243                    let response = client
1244                        .request(proto::BlameBuffer {
1245                            project_id: project_id.to_proto(),
1246                            buffer_id: buffer_id.into(),
1247                            version: serialize_version(&version),
1248                        })
1249                        .await?;
1250                    Ok(deserialize_blame_buffer_response(response))
1251                }
1252            }
1253        })
1254    }
1255
1256    pub fn file_history(
1257        &self,
1258        repo: &Entity<Repository>,
1259        path: RepoPath,
1260        cx: &mut App,
1261    ) -> Task<Result<git::repository::FileHistory>> {
1262        let rx = repo.update(cx, |repo, _| repo.file_history(path));
1263
1264        cx.spawn(|_: &mut AsyncApp| async move { rx.await? })
1265    }
1266
1267    pub fn file_history_paginated(
1268        &self,
1269        repo: &Entity<Repository>,
1270        path: RepoPath,
1271        skip: usize,
1272        limit: Option<usize>,
1273        cx: &mut App,
1274    ) -> Task<Result<git::repository::FileHistory>> {
1275        let rx = repo.update(cx, |repo, _| repo.file_history_paginated(path, skip, limit));
1276
1277        cx.spawn(|_: &mut AsyncApp| async move { rx.await? })
1278    }
1279
1280    pub fn get_permalink_to_line(
1281        &self,
1282        buffer: &Entity<Buffer>,
1283        selection: Range<u32>,
1284        cx: &mut App,
1285    ) -> Task<Result<url::Url>> {
1286        let Some(file) = File::from_dyn(buffer.read(cx).file()) else {
1287            return Task::ready(Err(anyhow!("buffer has no file")));
1288        };
1289
1290        let Some((repo, repo_path)) = self.repository_and_path_for_project_path(
1291            &(file.worktree.read(cx).id(), file.path.clone()).into(),
1292            cx,
1293        ) else {
1294            // If we're not in a Git repo, check whether this is a Rust source
1295            // file in the Cargo registry (presumably opened with go-to-definition
1296            // from a normal Rust file). If so, we can put together a permalink
1297            // using crate metadata.
1298            if buffer
1299                .read(cx)
1300                .language()
1301                .is_none_or(|lang| lang.name() != "Rust")
1302            {
1303                return Task::ready(Err(anyhow!("no permalink available")));
1304            }
1305            let file_path = file.worktree.read(cx).absolutize(&file.path);
1306            return cx.spawn(async move |cx| {
1307                let provider_registry = cx.update(GitHostingProviderRegistry::default_global);
1308                get_permalink_in_rust_registry_src(provider_registry, file_path, selection)
1309                    .context("no permalink available")
1310            });
1311        };
1312
1313        let buffer_id = buffer.read(cx).remote_id();
1314        let branch = repo.read(cx).branch.clone();
1315        let remote = branch
1316            .as_ref()
1317            .and_then(|b| b.upstream.as_ref())
1318            .and_then(|b| b.remote_name())
1319            .unwrap_or("origin")
1320            .to_string();
1321
1322        let rx = repo.update(cx, |repo, _| {
1323            repo.send_job(None, move |state, cx| async move {
1324                match state {
1325                    RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
1326                        let origin_url = backend
1327                            .remote_url(&remote)
1328                            .await
1329                            .with_context(|| format!("remote \"{remote}\" not found"))?;
1330
1331                        let sha = backend.head_sha().await.context("reading HEAD SHA")?;
1332
1333                        let provider_registry =
1334                            cx.update(GitHostingProviderRegistry::default_global);
1335
1336                        let (provider, remote) =
1337                            parse_git_remote_url(provider_registry, &origin_url)
1338                                .context("parsing Git remote URL")?;
1339
1340                        Ok(provider.build_permalink(
1341                            remote,
1342                            BuildPermalinkParams::new(&sha, &repo_path, Some(selection)),
1343                        ))
1344                    }
1345                    RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
1346                        let response = client
1347                            .request(proto::GetPermalinkToLine {
1348                                project_id: project_id.to_proto(),
1349                                buffer_id: buffer_id.into(),
1350                                selection: Some(proto::Range {
1351                                    start: selection.start as u64,
1352                                    end: selection.end as u64,
1353                                }),
1354                            })
1355                            .await?;
1356
1357                        url::Url::parse(&response.permalink).context("failed to parse permalink")
1358                    }
1359                }
1360            })
1361        });
1362        cx.spawn(|_: &mut AsyncApp| async move { rx.await? })
1363    }
1364
1365    fn downstream_client(&self) -> Option<(AnyProtoClient, ProjectId)> {
1366        match &self.state {
1367            GitStoreState::Local {
1368                downstream: downstream_client,
1369                ..
1370            } => downstream_client
1371                .as_ref()
1372                .map(|state| (state.client.clone(), state.project_id)),
1373            GitStoreState::Remote {
1374                downstream: downstream_client,
1375                ..
1376            } => downstream_client.clone(),
1377        }
1378    }
1379
1380    fn upstream_client(&self) -> Option<AnyProtoClient> {
1381        match &self.state {
1382            GitStoreState::Local { .. } => None,
1383            GitStoreState::Remote {
1384                upstream_client, ..
1385            } => Some(upstream_client.clone()),
1386        }
1387    }
1388
1389    fn on_worktree_store_event(
1390        &mut self,
1391        worktree_store: Entity<WorktreeStore>,
1392        event: &WorktreeStoreEvent,
1393        cx: &mut Context<Self>,
1394    ) {
1395        let GitStoreState::Local {
1396            project_environment,
1397            downstream,
1398            next_repository_id,
1399            fs,
1400        } = &self.state
1401        else {
1402            return;
1403        };
1404
1405        match event {
1406            WorktreeStoreEvent::WorktreeUpdatedEntries(worktree_id, updated_entries) => {
1407                if let Some(worktree) = self
1408                    .worktree_store
1409                    .read(cx)
1410                    .worktree_for_id(*worktree_id, cx)
1411                {
1412                    let paths_by_git_repo =
1413                        self.process_updated_entries(&worktree, updated_entries, cx);
1414                    let downstream = downstream
1415                        .as_ref()
1416                        .map(|downstream| downstream.updates_tx.clone());
1417                    cx.spawn(async move |_, cx| {
1418                        let paths_by_git_repo = paths_by_git_repo.await;
1419                        for (repo, paths) in paths_by_git_repo {
1420                            repo.update(cx, |repo, cx| {
1421                                repo.paths_changed(paths, downstream.clone(), cx);
1422                            });
1423                        }
1424                    })
1425                    .detach();
1426                }
1427            }
1428            WorktreeStoreEvent::WorktreeUpdatedGitRepositories(worktree_id, changed_repos) => {
1429                let Some(worktree) = worktree_store.read(cx).worktree_for_id(*worktree_id, cx)
1430                else {
1431                    return;
1432                };
1433                if !worktree.read(cx).is_visible() {
1434                    log::debug!(
1435                        "not adding repositories for local worktree {:?} because it's not visible",
1436                        worktree.read(cx).abs_path()
1437                    );
1438                    return;
1439                }
1440                self.update_repositories_from_worktree(
1441                    *worktree_id,
1442                    project_environment.clone(),
1443                    next_repository_id.clone(),
1444                    downstream
1445                        .as_ref()
1446                        .map(|downstream| downstream.updates_tx.clone()),
1447                    changed_repos.clone(),
1448                    fs.clone(),
1449                    cx,
1450                );
1451                self.local_worktree_git_repos_changed(worktree, changed_repos, cx);
1452            }
1453            WorktreeStoreEvent::WorktreeRemoved(_entity_id, worktree_id) => {
1454                let repos_without_worktree: Vec<RepositoryId> = self
1455                    .worktree_ids
1456                    .iter_mut()
1457                    .filter_map(|(repo_id, worktree_ids)| {
1458                        worktree_ids.remove(worktree_id);
1459                        if worktree_ids.is_empty() {
1460                            Some(*repo_id)
1461                        } else {
1462                            None
1463                        }
1464                    })
1465                    .collect();
1466                let is_active_repo_removed = repos_without_worktree
1467                    .iter()
1468                    .any(|repo_id| self.active_repo_id == Some(*repo_id));
1469
1470                for repo_id in repos_without_worktree {
1471                    self.repositories.remove(&repo_id);
1472                    self.worktree_ids.remove(&repo_id);
1473                    if let Some(updates_tx) =
1474                        downstream.as_ref().map(|downstream| &downstream.updates_tx)
1475                    {
1476                        updates_tx
1477                            .unbounded_send(DownstreamUpdate::RemoveRepository(repo_id))
1478                            .ok();
1479                    }
1480                }
1481
1482                if is_active_repo_removed {
1483                    if let Some((&repo_id, _)) = self.repositories.iter().next() {
1484                        self.active_repo_id = Some(repo_id);
1485                        cx.emit(GitStoreEvent::ActiveRepositoryChanged(Some(repo_id)));
1486                    } else {
1487                        self.active_repo_id = None;
1488                        cx.emit(GitStoreEvent::ActiveRepositoryChanged(None));
1489                    }
1490                }
1491            }
1492            _ => {}
1493        }
1494    }
1495    fn on_repository_event(
1496        &mut self,
1497        repo: Entity<Repository>,
1498        event: &RepositoryEvent,
1499        cx: &mut Context<Self>,
1500    ) {
1501        let id = repo.read(cx).id;
1502        let repo_snapshot = repo.read(cx).snapshot.clone();
1503        for (buffer_id, diff) in self.diffs.iter() {
1504            if let Some((buffer_repo, repo_path)) =
1505                self.repository_and_path_for_buffer_id(*buffer_id, cx)
1506                && buffer_repo == repo
1507            {
1508                diff.update(cx, |diff, cx| {
1509                    if let Some(conflict_set) = &diff.conflict_set {
1510                        let conflict_status_changed =
1511                            conflict_set.update(cx, |conflict_set, cx| {
1512                                let has_conflict = repo_snapshot.has_conflict(&repo_path);
1513                                conflict_set.set_has_conflict(has_conflict, cx)
1514                            })?;
1515                        if conflict_status_changed {
1516                            let buffer_store = self.buffer_store.read(cx);
1517                            if let Some(buffer) = buffer_store.get(*buffer_id) {
1518                                let _ = diff
1519                                    .reparse_conflict_markers(buffer.read(cx).text_snapshot(), cx);
1520                            }
1521                        }
1522                    }
1523                    anyhow::Ok(())
1524                })
1525                .ok();
1526            }
1527        }
1528        cx.emit(GitStoreEvent::RepositoryUpdated(
1529            id,
1530            event.clone(),
1531            self.active_repo_id == Some(id),
1532        ))
1533    }
1534
1535    fn on_jobs_updated(&mut self, _: Entity<Repository>, _: &JobsUpdated, cx: &mut Context<Self>) {
1536        cx.emit(GitStoreEvent::JobsUpdated)
1537    }
1538
1539    /// Update our list of repositories and schedule git scans in response to a notification from a worktree,
1540    fn update_repositories_from_worktree(
1541        &mut self,
1542        worktree_id: WorktreeId,
1543        project_environment: Entity<ProjectEnvironment>,
1544        next_repository_id: Arc<AtomicU64>,
1545        updates_tx: Option<mpsc::UnboundedSender<DownstreamUpdate>>,
1546        updated_git_repositories: UpdatedGitRepositoriesSet,
1547        fs: Arc<dyn Fs>,
1548        cx: &mut Context<Self>,
1549    ) {
1550        let mut removed_ids = Vec::new();
1551        for update in updated_git_repositories.iter() {
1552            if let Some((id, existing)) = self.repositories.iter().find(|(_, repo)| {
1553                let existing_work_directory_abs_path =
1554                    repo.read(cx).work_directory_abs_path.clone();
1555                Some(&existing_work_directory_abs_path)
1556                    == update.old_work_directory_abs_path.as_ref()
1557                    || Some(&existing_work_directory_abs_path)
1558                        == update.new_work_directory_abs_path.as_ref()
1559            }) {
1560                let repo_id = *id;
1561                if let Some(new_work_directory_abs_path) =
1562                    update.new_work_directory_abs_path.clone()
1563                {
1564                    self.worktree_ids
1565                        .entry(repo_id)
1566                        .or_insert_with(HashSet::new)
1567                        .insert(worktree_id);
1568                    existing.update(cx, |existing, cx| {
1569                        existing.snapshot.work_directory_abs_path = new_work_directory_abs_path;
1570                        existing.schedule_scan(updates_tx.clone(), cx);
1571                    });
1572                } else {
1573                    if let Some(worktree_ids) = self.worktree_ids.get_mut(&repo_id) {
1574                        worktree_ids.remove(&worktree_id);
1575                        if worktree_ids.is_empty() {
1576                            removed_ids.push(repo_id);
1577                        }
1578                    }
1579                }
1580            } else if let UpdatedGitRepository {
1581                new_work_directory_abs_path: Some(work_directory_abs_path),
1582                dot_git_abs_path: Some(dot_git_abs_path),
1583                repository_dir_abs_path: Some(repository_dir_abs_path),
1584                common_dir_abs_path: Some(common_dir_abs_path),
1585                ..
1586            } = update
1587            {
1588                let original_repo_abs_path: Arc<Path> = git::repository::original_repo_path(
1589                    work_directory_abs_path,
1590                    common_dir_abs_path,
1591                    repository_dir_abs_path,
1592                )
1593                .into();
1594                let id = RepositoryId(next_repository_id.fetch_add(1, atomic::Ordering::Release));
1595                let is_trusted = TrustedWorktrees::try_get_global(cx)
1596                    .map(|trusted_worktrees| {
1597                        trusted_worktrees.update(cx, |trusted_worktrees, cx| {
1598                            trusted_worktrees.can_trust(&self.worktree_store, worktree_id, cx)
1599                        })
1600                    })
1601                    .unwrap_or(false);
1602                let git_store = cx.weak_entity();
1603                let repo = cx.new(|cx| {
1604                    let mut repo = Repository::local(
1605                        id,
1606                        work_directory_abs_path.clone(),
1607                        original_repo_abs_path.clone(),
1608                        dot_git_abs_path.clone(),
1609                        project_environment.downgrade(),
1610                        fs.clone(),
1611                        is_trusted,
1612                        git_store,
1613                        cx,
1614                    );
1615                    if let Some(updates_tx) = updates_tx.as_ref() {
1616                        // trigger an empty `UpdateRepository` to ensure remote active_repo_id is set correctly
1617                        updates_tx
1618                            .unbounded_send(DownstreamUpdate::UpdateRepository(repo.snapshot()))
1619                            .ok();
1620                    }
1621                    repo.schedule_scan(updates_tx.clone(), cx);
1622                    repo
1623                });
1624                self._subscriptions
1625                    .push(cx.subscribe(&repo, Self::on_repository_event));
1626                self._subscriptions
1627                    .push(cx.subscribe(&repo, Self::on_jobs_updated));
1628                self.repositories.insert(id, repo);
1629                self.worktree_ids.insert(id, HashSet::from([worktree_id]));
1630                cx.emit(GitStoreEvent::RepositoryAdded);
1631                self.active_repo_id.get_or_insert_with(|| {
1632                    cx.emit(GitStoreEvent::ActiveRepositoryChanged(Some(id)));
1633                    id
1634                });
1635            }
1636        }
1637
1638        for id in removed_ids {
1639            if self.active_repo_id == Some(id) {
1640                self.active_repo_id = None;
1641                cx.emit(GitStoreEvent::ActiveRepositoryChanged(None));
1642            }
1643            self.repositories.remove(&id);
1644            if let Some(updates_tx) = updates_tx.as_ref() {
1645                updates_tx
1646                    .unbounded_send(DownstreamUpdate::RemoveRepository(id))
1647                    .ok();
1648            }
1649        }
1650    }
1651
1652    fn on_trusted_worktrees_event(
1653        &mut self,
1654        _: Entity<TrustedWorktreesStore>,
1655        event: &TrustedWorktreesEvent,
1656        cx: &mut Context<Self>,
1657    ) {
1658        if !matches!(self.state, GitStoreState::Local { .. }) {
1659            return;
1660        }
1661
1662        let (is_trusted, event_paths) = match event {
1663            TrustedWorktreesEvent::Trusted(_, trusted_paths) => (true, trusted_paths),
1664            TrustedWorktreesEvent::Restricted(_, restricted_paths) => (false, restricted_paths),
1665        };
1666
1667        for (repo_id, worktree_ids) in &self.worktree_ids {
1668            if worktree_ids
1669                .iter()
1670                .any(|worktree_id| event_paths.contains(&PathTrust::Worktree(*worktree_id)))
1671            {
1672                if let Some(repo) = self.repositories.get(repo_id) {
1673                    let repository_state = repo.read(cx).repository_state.clone();
1674                    cx.background_spawn(async move {
1675                        if let Ok(RepositoryState::Local(state)) = repository_state.await {
1676                            state.backend.set_trusted(is_trusted);
1677                        }
1678                    })
1679                    .detach();
1680                }
1681            }
1682        }
1683    }
1684
1685    fn on_buffer_store_event(
1686        &mut self,
1687        _: Entity<BufferStore>,
1688        event: &BufferStoreEvent,
1689        cx: &mut Context<Self>,
1690    ) {
1691        match event {
1692            BufferStoreEvent::BufferAdded(buffer) => {
1693                cx.subscribe(buffer, |this, buffer, event, cx| {
1694                    if let BufferEvent::LanguageChanged(_) = event {
1695                        let buffer_id = buffer.read(cx).remote_id();
1696                        if let Some(diff_state) = this.diffs.get(&buffer_id) {
1697                            diff_state.update(cx, |diff_state, cx| {
1698                                diff_state.buffer_language_changed(buffer, cx);
1699                            });
1700                        }
1701                    }
1702                })
1703                .detach();
1704            }
1705            BufferStoreEvent::SharedBufferClosed(peer_id, buffer_id) => {
1706                if let Some(diffs) = self.shared_diffs.get_mut(peer_id) {
1707                    diffs.remove(buffer_id);
1708                }
1709            }
1710            BufferStoreEvent::BufferDropped(buffer_id) => {
1711                self.diffs.remove(buffer_id);
1712                for diffs in self.shared_diffs.values_mut() {
1713                    diffs.remove(buffer_id);
1714                }
1715            }
1716            BufferStoreEvent::BufferChangedFilePath { buffer, .. } => {
1717                // Whenever a buffer's file path changes, it's possible that the
1718                // new path is actually a path that is being tracked by a git
1719                // repository. In that case, we'll want to update the buffer's
1720                // `BufferDiffState`, in case it already has one.
1721                let buffer_id = buffer.read(cx).remote_id();
1722                let diff_state = self.diffs.get(&buffer_id);
1723                let repo = self.repository_and_path_for_buffer_id(buffer_id, cx);
1724
1725                if let Some(diff_state) = diff_state
1726                    && let Some((repo, repo_path)) = repo
1727                {
1728                    let buffer = buffer.clone();
1729                    let diff_state = diff_state.clone();
1730
1731                    cx.spawn(async move |_git_store, cx| {
1732                        async {
1733                            let diff_bases_change = repo
1734                                .update(cx, |repo, cx| {
1735                                    repo.load_committed_text(buffer_id, repo_path, cx)
1736                                })
1737                                .await?;
1738
1739                            diff_state.update(cx, |diff_state, cx| {
1740                                let buffer_snapshot = buffer.read(cx).text_snapshot();
1741                                diff_state.diff_bases_changed(
1742                                    buffer_snapshot,
1743                                    Some(diff_bases_change),
1744                                    cx,
1745                                );
1746                            });
1747                            anyhow::Ok(())
1748                        }
1749                        .await
1750                        .log_err();
1751                    })
1752                    .detach();
1753                }
1754            }
1755        }
1756    }
1757
1758    pub fn recalculate_buffer_diffs(
1759        &mut self,
1760        buffers: Vec<Entity<Buffer>>,
1761        cx: &mut Context<Self>,
1762    ) -> impl Future<Output = ()> + use<> {
1763        let mut futures = Vec::new();
1764        for buffer in buffers {
1765            if let Some(diff_state) = self.diffs.get_mut(&buffer.read(cx).remote_id()) {
1766                let buffer = buffer.read(cx).text_snapshot();
1767                diff_state.update(cx, |diff_state, cx| {
1768                    diff_state.recalculate_diffs(buffer.clone(), cx);
1769                    futures.extend(diff_state.wait_for_recalculation().map(FutureExt::boxed));
1770                });
1771                futures.push(diff_state.update(cx, |diff_state, cx| {
1772                    diff_state
1773                        .reparse_conflict_markers(buffer, cx)
1774                        .map(|_| {})
1775                        .boxed()
1776                }));
1777            }
1778        }
1779        async move {
1780            futures::future::join_all(futures).await;
1781        }
1782    }
1783
1784    fn on_buffer_diff_event(
1785        &mut self,
1786        diff: Entity<buffer_diff::BufferDiff>,
1787        event: &BufferDiffEvent,
1788        cx: &mut Context<Self>,
1789    ) {
1790        if let BufferDiffEvent::HunksStagedOrUnstaged(new_index_text) = event {
1791            let buffer_id = diff.read(cx).buffer_id;
1792            if let Some(diff_state) = self.diffs.get(&buffer_id) {
1793                let hunk_staging_operation_count = diff_state.update(cx, |diff_state, _| {
1794                    diff_state.hunk_staging_operation_count += 1;
1795                    diff_state.hunk_staging_operation_count
1796                });
1797                if let Some((repo, path)) = self.repository_and_path_for_buffer_id(buffer_id, cx) {
1798                    let recv = repo.update(cx, |repo, cx| {
1799                        log::debug!("hunks changed for {}", path.as_unix_str());
1800                        repo.spawn_set_index_text_job(
1801                            path,
1802                            new_index_text.as_ref().map(|rope| rope.to_string()),
1803                            Some(hunk_staging_operation_count),
1804                            cx,
1805                        )
1806                    });
1807                    let diff = diff.downgrade();
1808                    cx.spawn(async move |this, cx| {
1809                        if let Ok(Err(error)) = cx.background_spawn(recv).await {
1810                            diff.update(cx, |diff, cx| {
1811                                diff.clear_pending_hunks(cx);
1812                            })
1813                            .ok();
1814                            this.update(cx, |_, cx| cx.emit(GitStoreEvent::IndexWriteError(error)))
1815                                .ok();
1816                        }
1817                    })
1818                    .detach();
1819                }
1820            }
1821        }
1822    }
1823
1824    fn local_worktree_git_repos_changed(
1825        &mut self,
1826        worktree: Entity<Worktree>,
1827        changed_repos: &UpdatedGitRepositoriesSet,
1828        cx: &mut Context<Self>,
1829    ) {
1830        log::debug!("local worktree repos changed");
1831        debug_assert!(worktree.read(cx).is_local());
1832
1833        for repository in self.repositories.values() {
1834            repository.update(cx, |repository, cx| {
1835                let repo_abs_path = &repository.work_directory_abs_path;
1836                if changed_repos.iter().any(|update| {
1837                    update.old_work_directory_abs_path.as_ref() == Some(repo_abs_path)
1838                        || update.new_work_directory_abs_path.as_ref() == Some(repo_abs_path)
1839                }) {
1840                    repository.reload_buffer_diff_bases(cx);
1841                }
1842            });
1843        }
1844    }
1845
1846    pub fn repositories(&self) -> &HashMap<RepositoryId, Entity<Repository>> {
1847        &self.repositories
1848    }
1849
1850    /// Returns the original (main) repository working directory for the given worktree.
1851    /// For normal checkouts this equals the worktree's own path; for linked
1852    /// worktrees it points back to the original repo.
1853    pub fn original_repo_path_for_worktree(
1854        &self,
1855        worktree_id: WorktreeId,
1856        cx: &App,
1857    ) -> Option<Arc<Path>> {
1858        self.active_repo_id
1859            .iter()
1860            .chain(self.worktree_ids.keys())
1861            .find(|repo_id| {
1862                self.worktree_ids
1863                    .get(repo_id)
1864                    .is_some_and(|ids| ids.contains(&worktree_id))
1865            })
1866            .and_then(|repo_id| self.repositories.get(repo_id))
1867            .map(|repo| repo.read(cx).snapshot().original_repo_abs_path)
1868    }
1869
1870    pub fn status_for_buffer_id(&self, buffer_id: BufferId, cx: &App) -> Option<FileStatus> {
1871        let (repo, path) = self.repository_and_path_for_buffer_id(buffer_id, cx)?;
1872        let status = repo.read(cx).snapshot.status_for_path(&path)?;
1873        Some(status.status)
1874    }
1875
1876    pub fn repository_and_path_for_buffer_id(
1877        &self,
1878        buffer_id: BufferId,
1879        cx: &App,
1880    ) -> Option<(Entity<Repository>, RepoPath)> {
1881        let buffer = self.buffer_store.read(cx).get(buffer_id)?;
1882        let project_path = buffer.read(cx).project_path(cx)?;
1883        self.repository_and_path_for_project_path(&project_path, cx)
1884    }
1885
1886    pub fn repository_and_path_for_project_path(
1887        &self,
1888        path: &ProjectPath,
1889        cx: &App,
1890    ) -> Option<(Entity<Repository>, RepoPath)> {
1891        let abs_path = self.worktree_store.read(cx).absolutize(path, cx)?;
1892        self.repositories
1893            .values()
1894            .filter_map(|repo| {
1895                let repo_path = repo.read(cx).abs_path_to_repo_path(&abs_path)?;
1896                Some((repo.clone(), repo_path))
1897            })
1898            .max_by_key(|(repo, _)| repo.read(cx).work_directory_abs_path.clone())
1899    }
1900
1901    pub fn git_init(
1902        &self,
1903        path: Arc<Path>,
1904        fallback_branch_name: String,
1905        cx: &App,
1906    ) -> Task<Result<()>> {
1907        match &self.state {
1908            GitStoreState::Local { fs, .. } => {
1909                let fs = fs.clone();
1910                cx.background_executor()
1911                    .spawn(async move { fs.git_init(&path, fallback_branch_name).await })
1912            }
1913            GitStoreState::Remote {
1914                upstream_client,
1915                upstream_project_id: project_id,
1916                ..
1917            } => {
1918                let client = upstream_client.clone();
1919                let project_id = *project_id;
1920                cx.background_executor().spawn(async move {
1921                    client
1922                        .request(proto::GitInit {
1923                            project_id: project_id,
1924                            abs_path: path.to_string_lossy().into_owned(),
1925                            fallback_branch_name,
1926                        })
1927                        .await?;
1928                    Ok(())
1929                })
1930            }
1931        }
1932    }
1933
1934    pub fn git_clone(
1935        &self,
1936        repo: String,
1937        path: impl Into<Arc<std::path::Path>>,
1938        cx: &App,
1939    ) -> Task<Result<()>> {
1940        let path = path.into();
1941        match &self.state {
1942            GitStoreState::Local { fs, .. } => {
1943                let fs = fs.clone();
1944                cx.background_executor()
1945                    .spawn(async move { fs.git_clone(&repo, &path).await })
1946            }
1947            GitStoreState::Remote {
1948                upstream_client,
1949                upstream_project_id,
1950                ..
1951            } => {
1952                if upstream_client.is_via_collab() {
1953                    return Task::ready(Err(anyhow!(
1954                        "Git Clone isn't supported for project guests"
1955                    )));
1956                }
1957                let request = upstream_client.request(proto::GitClone {
1958                    project_id: *upstream_project_id,
1959                    abs_path: path.to_string_lossy().into_owned(),
1960                    remote_repo: repo,
1961                });
1962
1963                cx.background_spawn(async move {
1964                    let result = request.await?;
1965
1966                    match result.success {
1967                        true => Ok(()),
1968                        false => Err(anyhow!("Git Clone failed")),
1969                    }
1970                })
1971            }
1972        }
1973    }
1974
1975    async fn handle_update_repository(
1976        this: Entity<Self>,
1977        envelope: TypedEnvelope<proto::UpdateRepository>,
1978        mut cx: AsyncApp,
1979    ) -> Result<()> {
1980        this.update(&mut cx, |this, cx| {
1981            let path_style = this.worktree_store.read(cx).path_style();
1982            let mut update = envelope.payload;
1983
1984            let id = RepositoryId::from_proto(update.id);
1985            let client = this.upstream_client().context("no upstream client")?;
1986
1987            let original_repo_abs_path: Option<Arc<Path>> = update
1988                .original_repo_abs_path
1989                .as_deref()
1990                .map(|p| Path::new(p).into());
1991
1992            let mut repo_subscription = None;
1993            let repo = this.repositories.entry(id).or_insert_with(|| {
1994                let git_store = cx.weak_entity();
1995                let repo = cx.new(|cx| {
1996                    Repository::remote(
1997                        id,
1998                        Path::new(&update.abs_path).into(),
1999                        original_repo_abs_path.clone(),
2000                        path_style,
2001                        ProjectId(update.project_id),
2002                        client,
2003                        git_store,
2004                        cx,
2005                    )
2006                });
2007                repo_subscription = Some(cx.subscribe(&repo, Self::on_repository_event));
2008                cx.emit(GitStoreEvent::RepositoryAdded);
2009                repo
2010            });
2011            this._subscriptions.extend(repo_subscription);
2012
2013            repo.update(cx, {
2014                let update = update.clone();
2015                |repo, cx| repo.apply_remote_update(update, cx)
2016            })?;
2017
2018            this.active_repo_id.get_or_insert_with(|| {
2019                cx.emit(GitStoreEvent::ActiveRepositoryChanged(Some(id)));
2020                id
2021            });
2022
2023            if let Some((client, project_id)) = this.downstream_client() {
2024                update.project_id = project_id.to_proto();
2025                client.send(update).log_err();
2026            }
2027            Ok(())
2028        })
2029    }
2030
2031    async fn handle_remove_repository(
2032        this: Entity<Self>,
2033        envelope: TypedEnvelope<proto::RemoveRepository>,
2034        mut cx: AsyncApp,
2035    ) -> Result<()> {
2036        this.update(&mut cx, |this, cx| {
2037            let mut update = envelope.payload;
2038            let id = RepositoryId::from_proto(update.id);
2039            this.repositories.remove(&id);
2040            if let Some((client, project_id)) = this.downstream_client() {
2041                update.project_id = project_id.to_proto();
2042                client.send(update).log_err();
2043            }
2044            if this.active_repo_id == Some(id) {
2045                this.active_repo_id = None;
2046                cx.emit(GitStoreEvent::ActiveRepositoryChanged(None));
2047            }
2048            cx.emit(GitStoreEvent::RepositoryRemoved(id));
2049        });
2050        Ok(())
2051    }
2052
2053    async fn handle_git_init(
2054        this: Entity<Self>,
2055        envelope: TypedEnvelope<proto::GitInit>,
2056        cx: AsyncApp,
2057    ) -> Result<proto::Ack> {
2058        let path: Arc<Path> = PathBuf::from(envelope.payload.abs_path).into();
2059        let name = envelope.payload.fallback_branch_name;
2060        cx.update(|cx| this.read(cx).git_init(path, name, cx))
2061            .await?;
2062
2063        Ok(proto::Ack {})
2064    }
2065
2066    async fn handle_git_clone(
2067        this: Entity<Self>,
2068        envelope: TypedEnvelope<proto::GitClone>,
2069        cx: AsyncApp,
2070    ) -> Result<proto::GitCloneResponse> {
2071        let path: Arc<Path> = PathBuf::from(envelope.payload.abs_path).into();
2072        let repo_name = envelope.payload.remote_repo;
2073        let result = cx
2074            .update(|cx| this.read(cx).git_clone(repo_name, path, cx))
2075            .await;
2076
2077        Ok(proto::GitCloneResponse {
2078            success: result.is_ok(),
2079        })
2080    }
2081
2082    async fn handle_fetch(
2083        this: Entity<Self>,
2084        envelope: TypedEnvelope<proto::Fetch>,
2085        mut cx: AsyncApp,
2086    ) -> Result<proto::RemoteMessageResponse> {
2087        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2088        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2089        let fetch_options = FetchOptions::from_proto(envelope.payload.remote);
2090        let askpass_id = envelope.payload.askpass_id;
2091
2092        let askpass = make_remote_delegate(
2093            this,
2094            envelope.payload.project_id,
2095            repository_id,
2096            askpass_id,
2097            &mut cx,
2098        );
2099
2100        let remote_output = repository_handle
2101            .update(&mut cx, |repository_handle, cx| {
2102                repository_handle.fetch(fetch_options, askpass, cx)
2103            })
2104            .await??;
2105
2106        Ok(proto::RemoteMessageResponse {
2107            stdout: remote_output.stdout,
2108            stderr: remote_output.stderr,
2109        })
2110    }
2111
2112    async fn handle_push(
2113        this: Entity<Self>,
2114        envelope: TypedEnvelope<proto::Push>,
2115        mut cx: AsyncApp,
2116    ) -> Result<proto::RemoteMessageResponse> {
2117        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2118        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2119
2120        let askpass_id = envelope.payload.askpass_id;
2121        let askpass = make_remote_delegate(
2122            this,
2123            envelope.payload.project_id,
2124            repository_id,
2125            askpass_id,
2126            &mut cx,
2127        );
2128
2129        let options = envelope
2130            .payload
2131            .options
2132            .as_ref()
2133            .map(|_| match envelope.payload.options() {
2134                proto::push::PushOptions::SetUpstream => git::repository::PushOptions::SetUpstream,
2135                proto::push::PushOptions::Force => git::repository::PushOptions::Force,
2136            });
2137
2138        let branch_name = envelope.payload.branch_name.into();
2139        let remote_branch_name = envelope.payload.remote_branch_name.into();
2140        let remote_name = envelope.payload.remote_name.into();
2141
2142        let remote_output = repository_handle
2143            .update(&mut cx, |repository_handle, cx| {
2144                repository_handle.push(
2145                    branch_name,
2146                    remote_branch_name,
2147                    remote_name,
2148                    options,
2149                    askpass,
2150                    cx,
2151                )
2152            })
2153            .await??;
2154        Ok(proto::RemoteMessageResponse {
2155            stdout: remote_output.stdout,
2156            stderr: remote_output.stderr,
2157        })
2158    }
2159
2160    async fn handle_pull(
2161        this: Entity<Self>,
2162        envelope: TypedEnvelope<proto::Pull>,
2163        mut cx: AsyncApp,
2164    ) -> Result<proto::RemoteMessageResponse> {
2165        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2166        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2167        let askpass_id = envelope.payload.askpass_id;
2168        let askpass = make_remote_delegate(
2169            this,
2170            envelope.payload.project_id,
2171            repository_id,
2172            askpass_id,
2173            &mut cx,
2174        );
2175
2176        let branch_name = envelope.payload.branch_name.map(|name| name.into());
2177        let remote_name = envelope.payload.remote_name.into();
2178        let rebase = envelope.payload.rebase;
2179
2180        let remote_message = repository_handle
2181            .update(&mut cx, |repository_handle, cx| {
2182                repository_handle.pull(branch_name, remote_name, rebase, askpass, cx)
2183            })
2184            .await??;
2185
2186        Ok(proto::RemoteMessageResponse {
2187            stdout: remote_message.stdout,
2188            stderr: remote_message.stderr,
2189        })
2190    }
2191
2192    async fn handle_stage(
2193        this: Entity<Self>,
2194        envelope: TypedEnvelope<proto::Stage>,
2195        mut cx: AsyncApp,
2196    ) -> Result<proto::Ack> {
2197        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2198        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2199
2200        let entries = envelope
2201            .payload
2202            .paths
2203            .into_iter()
2204            .map(|path| RepoPath::new(&path))
2205            .collect::<Result<Vec<_>>>()?;
2206
2207        repository_handle
2208            .update(&mut cx, |repository_handle, cx| {
2209                repository_handle.stage_entries(entries, cx)
2210            })
2211            .await?;
2212        Ok(proto::Ack {})
2213    }
2214
2215    async fn handle_unstage(
2216        this: Entity<Self>,
2217        envelope: TypedEnvelope<proto::Unstage>,
2218        mut cx: AsyncApp,
2219    ) -> Result<proto::Ack> {
2220        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2221        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2222
2223        let entries = envelope
2224            .payload
2225            .paths
2226            .into_iter()
2227            .map(|path| RepoPath::new(&path))
2228            .collect::<Result<Vec<_>>>()?;
2229
2230        repository_handle
2231            .update(&mut cx, |repository_handle, cx| {
2232                repository_handle.unstage_entries(entries, cx)
2233            })
2234            .await?;
2235
2236        Ok(proto::Ack {})
2237    }
2238
2239    async fn handle_stash(
2240        this: Entity<Self>,
2241        envelope: TypedEnvelope<proto::Stash>,
2242        mut cx: AsyncApp,
2243    ) -> Result<proto::Ack> {
2244        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2245        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2246
2247        let entries = envelope
2248            .payload
2249            .paths
2250            .into_iter()
2251            .map(|path| RepoPath::new(&path))
2252            .collect::<Result<Vec<_>>>()?;
2253
2254        repository_handle
2255            .update(&mut cx, |repository_handle, cx| {
2256                repository_handle.stash_entries(entries, cx)
2257            })
2258            .await?;
2259
2260        Ok(proto::Ack {})
2261    }
2262
2263    async fn handle_stash_pop(
2264        this: Entity<Self>,
2265        envelope: TypedEnvelope<proto::StashPop>,
2266        mut cx: AsyncApp,
2267    ) -> Result<proto::Ack> {
2268        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2269        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2270        let stash_index = envelope.payload.stash_index.map(|i| i as usize);
2271
2272        repository_handle
2273            .update(&mut cx, |repository_handle, cx| {
2274                repository_handle.stash_pop(stash_index, cx)
2275            })
2276            .await?;
2277
2278        Ok(proto::Ack {})
2279    }
2280
2281    async fn handle_stash_apply(
2282        this: Entity<Self>,
2283        envelope: TypedEnvelope<proto::StashApply>,
2284        mut cx: AsyncApp,
2285    ) -> Result<proto::Ack> {
2286        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2287        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2288        let stash_index = envelope.payload.stash_index.map(|i| i as usize);
2289
2290        repository_handle
2291            .update(&mut cx, |repository_handle, cx| {
2292                repository_handle.stash_apply(stash_index, cx)
2293            })
2294            .await?;
2295
2296        Ok(proto::Ack {})
2297    }
2298
2299    async fn handle_stash_drop(
2300        this: Entity<Self>,
2301        envelope: TypedEnvelope<proto::StashDrop>,
2302        mut cx: AsyncApp,
2303    ) -> Result<proto::Ack> {
2304        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2305        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2306        let stash_index = envelope.payload.stash_index.map(|i| i as usize);
2307
2308        repository_handle
2309            .update(&mut cx, |repository_handle, cx| {
2310                repository_handle.stash_drop(stash_index, cx)
2311            })
2312            .await??;
2313
2314        Ok(proto::Ack {})
2315    }
2316
2317    async fn handle_set_index_text(
2318        this: Entity<Self>,
2319        envelope: TypedEnvelope<proto::SetIndexText>,
2320        mut cx: AsyncApp,
2321    ) -> Result<proto::Ack> {
2322        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2323        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2324        let repo_path = RepoPath::from_proto(&envelope.payload.path)?;
2325
2326        repository_handle
2327            .update(&mut cx, |repository_handle, cx| {
2328                repository_handle.spawn_set_index_text_job(
2329                    repo_path,
2330                    envelope.payload.text,
2331                    None,
2332                    cx,
2333                )
2334            })
2335            .await??;
2336        Ok(proto::Ack {})
2337    }
2338
2339    async fn handle_run_hook(
2340        this: Entity<Self>,
2341        envelope: TypedEnvelope<proto::RunGitHook>,
2342        mut cx: AsyncApp,
2343    ) -> Result<proto::Ack> {
2344        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2345        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2346        let hook = RunHook::from_proto(envelope.payload.hook).context("invalid hook")?;
2347        repository_handle
2348            .update(&mut cx, |repository_handle, cx| {
2349                repository_handle.run_hook(hook, cx)
2350            })
2351            .await??;
2352        Ok(proto::Ack {})
2353    }
2354
2355    async fn handle_commit(
2356        this: Entity<Self>,
2357        envelope: TypedEnvelope<proto::Commit>,
2358        mut cx: AsyncApp,
2359    ) -> Result<proto::Ack> {
2360        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2361        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2362        let askpass_id = envelope.payload.askpass_id;
2363
2364        let askpass = make_remote_delegate(
2365            this,
2366            envelope.payload.project_id,
2367            repository_id,
2368            askpass_id,
2369            &mut cx,
2370        );
2371
2372        let message = SharedString::from(envelope.payload.message);
2373        let name = envelope.payload.name.map(SharedString::from);
2374        let email = envelope.payload.email.map(SharedString::from);
2375        let options = envelope.payload.options.unwrap_or_default();
2376
2377        repository_handle
2378            .update(&mut cx, |repository_handle, cx| {
2379                repository_handle.commit(
2380                    message,
2381                    name.zip(email),
2382                    CommitOptions {
2383                        amend: options.amend,
2384                        signoff: options.signoff,
2385                        allow_empty: options.allow_empty,
2386                    },
2387                    askpass,
2388                    cx,
2389                )
2390            })
2391            .await??;
2392        Ok(proto::Ack {})
2393    }
2394
2395    async fn handle_get_remotes(
2396        this: Entity<Self>,
2397        envelope: TypedEnvelope<proto::GetRemotes>,
2398        mut cx: AsyncApp,
2399    ) -> Result<proto::GetRemotesResponse> {
2400        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2401        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2402
2403        let branch_name = envelope.payload.branch_name;
2404        let is_push = envelope.payload.is_push;
2405
2406        let remotes = repository_handle
2407            .update(&mut cx, |repository_handle, _| {
2408                repository_handle.get_remotes(branch_name, is_push)
2409            })
2410            .await??;
2411
2412        Ok(proto::GetRemotesResponse {
2413            remotes: remotes
2414                .into_iter()
2415                .map(|remotes| proto::get_remotes_response::Remote {
2416                    name: remotes.name.to_string(),
2417                })
2418                .collect::<Vec<_>>(),
2419        })
2420    }
2421
2422    async fn handle_get_worktrees(
2423        this: Entity<Self>,
2424        envelope: TypedEnvelope<proto::GitGetWorktrees>,
2425        mut cx: AsyncApp,
2426    ) -> Result<proto::GitWorktreesResponse> {
2427        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2428        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2429
2430        let worktrees = repository_handle
2431            .update(&mut cx, |repository_handle, _| {
2432                repository_handle.worktrees()
2433            })
2434            .await??;
2435
2436        Ok(proto::GitWorktreesResponse {
2437            worktrees: worktrees
2438                .into_iter()
2439                .map(|worktree| worktree_to_proto(&worktree))
2440                .collect::<Vec<_>>(),
2441        })
2442    }
2443
2444    async fn handle_create_worktree(
2445        this: Entity<Self>,
2446        envelope: TypedEnvelope<proto::GitCreateWorktree>,
2447        mut cx: AsyncApp,
2448    ) -> Result<proto::Ack> {
2449        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2450        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2451        let directory = PathBuf::from(envelope.payload.directory);
2452        let name = envelope.payload.name;
2453        let commit = envelope.payload.commit;
2454        let use_existing_branch = envelope.payload.use_existing_branch;
2455        let target = if name.is_empty() {
2456            CreateWorktreeTarget::Detached { base_sha: commit }
2457        } else if use_existing_branch {
2458            CreateWorktreeTarget::ExistingBranch { branch_name: name }
2459        } else {
2460            CreateWorktreeTarget::NewBranch {
2461                branch_name: name,
2462                base_sha: commit,
2463            }
2464        };
2465
2466        repository_handle
2467            .update(&mut cx, |repository_handle, _| {
2468                repository_handle.create_worktree(target, directory)
2469            })
2470            .await??;
2471
2472        Ok(proto::Ack {})
2473    }
2474
2475    async fn handle_remove_worktree(
2476        this: Entity<Self>,
2477        envelope: TypedEnvelope<proto::GitRemoveWorktree>,
2478        mut cx: AsyncApp,
2479    ) -> Result<proto::Ack> {
2480        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2481        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2482        let path = PathBuf::from(envelope.payload.path);
2483        let force = envelope.payload.force;
2484
2485        repository_handle
2486            .update(&mut cx, |repository_handle, _| {
2487                repository_handle.remove_worktree(path, force)
2488            })
2489            .await??;
2490
2491        Ok(proto::Ack {})
2492    }
2493
2494    async fn handle_rename_worktree(
2495        this: Entity<Self>,
2496        envelope: TypedEnvelope<proto::GitRenameWorktree>,
2497        mut cx: AsyncApp,
2498    ) -> Result<proto::Ack> {
2499        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2500        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2501        let old_path = PathBuf::from(envelope.payload.old_path);
2502        let new_path = PathBuf::from(envelope.payload.new_path);
2503
2504        repository_handle
2505            .update(&mut cx, |repository_handle, _| {
2506                repository_handle.rename_worktree(old_path, new_path)
2507            })
2508            .await??;
2509
2510        Ok(proto::Ack {})
2511    }
2512
2513    async fn handle_get_head_sha(
2514        this: Entity<Self>,
2515        envelope: TypedEnvelope<proto::GitGetHeadSha>,
2516        mut cx: AsyncApp,
2517    ) -> Result<proto::GitGetHeadShaResponse> {
2518        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2519        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2520
2521        let head_sha = repository_handle
2522            .update(&mut cx, |repository_handle, _| repository_handle.head_sha())
2523            .await??;
2524
2525        Ok(proto::GitGetHeadShaResponse { sha: head_sha })
2526    }
2527
2528    async fn handle_edit_ref(
2529        this: Entity<Self>,
2530        envelope: TypedEnvelope<proto::GitEditRef>,
2531        mut cx: AsyncApp,
2532    ) -> Result<proto::Ack> {
2533        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2534        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2535        let ref_name = envelope.payload.ref_name;
2536        let commit = match envelope.payload.action {
2537            Some(proto::git_edit_ref::Action::UpdateToCommit(sha)) => Some(sha),
2538            Some(proto::git_edit_ref::Action::Delete(_)) => None,
2539            None => anyhow::bail!("GitEditRef missing action"),
2540        };
2541
2542        repository_handle
2543            .update(&mut cx, |repository_handle, _| {
2544                repository_handle.edit_ref(ref_name, commit)
2545            })
2546            .await??;
2547
2548        Ok(proto::Ack {})
2549    }
2550
2551    async fn handle_repair_worktrees(
2552        this: Entity<Self>,
2553        envelope: TypedEnvelope<proto::GitRepairWorktrees>,
2554        mut cx: AsyncApp,
2555    ) -> Result<proto::Ack> {
2556        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2557        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2558
2559        repository_handle
2560            .update(&mut cx, |repository_handle, _| {
2561                repository_handle.repair_worktrees()
2562            })
2563            .await??;
2564
2565        Ok(proto::Ack {})
2566    }
2567
2568    async fn handle_get_branches(
2569        this: Entity<Self>,
2570        envelope: TypedEnvelope<proto::GitGetBranches>,
2571        mut cx: AsyncApp,
2572    ) -> Result<proto::GitBranchesResponse> {
2573        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2574        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2575
2576        let branches = repository_handle
2577            .update(&mut cx, |repository_handle, _| repository_handle.branches())
2578            .await??;
2579
2580        Ok(proto::GitBranchesResponse {
2581            branches: branches
2582                .into_iter()
2583                .map(|branch| branch_to_proto(&branch))
2584                .collect::<Vec<_>>(),
2585        })
2586    }
2587    async fn handle_get_default_branch(
2588        this: Entity<Self>,
2589        envelope: TypedEnvelope<proto::GetDefaultBranch>,
2590        mut cx: AsyncApp,
2591    ) -> Result<proto::GetDefaultBranchResponse> {
2592        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2593        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2594
2595        let branch = repository_handle
2596            .update(&mut cx, |repository_handle, _| {
2597                repository_handle.default_branch(false)
2598            })
2599            .await??
2600            .map(Into::into);
2601
2602        Ok(proto::GetDefaultBranchResponse { branch })
2603    }
2604    async fn handle_create_branch(
2605        this: Entity<Self>,
2606        envelope: TypedEnvelope<proto::GitCreateBranch>,
2607        mut cx: AsyncApp,
2608    ) -> Result<proto::Ack> {
2609        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2610        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2611        let branch_name = envelope.payload.branch_name;
2612
2613        repository_handle
2614            .update(&mut cx, |repository_handle, _| {
2615                repository_handle.create_branch(branch_name, None)
2616            })
2617            .await??;
2618
2619        Ok(proto::Ack {})
2620    }
2621
2622    async fn handle_change_branch(
2623        this: Entity<Self>,
2624        envelope: TypedEnvelope<proto::GitChangeBranch>,
2625        mut cx: AsyncApp,
2626    ) -> Result<proto::Ack> {
2627        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2628        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2629        let branch_name = envelope.payload.branch_name;
2630
2631        repository_handle
2632            .update(&mut cx, |repository_handle, _| {
2633                repository_handle.change_branch(branch_name)
2634            })
2635            .await??;
2636
2637        Ok(proto::Ack {})
2638    }
2639
2640    async fn handle_rename_branch(
2641        this: Entity<Self>,
2642        envelope: TypedEnvelope<proto::GitRenameBranch>,
2643        mut cx: AsyncApp,
2644    ) -> Result<proto::Ack> {
2645        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2646        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2647        let branch = envelope.payload.branch;
2648        let new_name = envelope.payload.new_name;
2649
2650        repository_handle
2651            .update(&mut cx, |repository_handle, _| {
2652                repository_handle.rename_branch(branch, new_name)
2653            })
2654            .await??;
2655
2656        Ok(proto::Ack {})
2657    }
2658
2659    async fn handle_create_remote(
2660        this: Entity<Self>,
2661        envelope: TypedEnvelope<proto::GitCreateRemote>,
2662        mut cx: AsyncApp,
2663    ) -> Result<proto::Ack> {
2664        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2665        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2666        let remote_name = envelope.payload.remote_name;
2667        let remote_url = envelope.payload.remote_url;
2668
2669        repository_handle
2670            .update(&mut cx, |repository_handle, _| {
2671                repository_handle.create_remote(remote_name, remote_url)
2672            })
2673            .await??;
2674
2675        Ok(proto::Ack {})
2676    }
2677
2678    async fn handle_delete_branch(
2679        this: Entity<Self>,
2680        envelope: TypedEnvelope<proto::GitDeleteBranch>,
2681        mut cx: AsyncApp,
2682    ) -> Result<proto::Ack> {
2683        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2684        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2685        let is_remote = envelope.payload.is_remote;
2686        let branch_name = envelope.payload.branch_name;
2687
2688        repository_handle
2689            .update(&mut cx, |repository_handle, _| {
2690                repository_handle.delete_branch(is_remote, branch_name)
2691            })
2692            .await??;
2693
2694        Ok(proto::Ack {})
2695    }
2696
2697    async fn handle_remove_remote(
2698        this: Entity<Self>,
2699        envelope: TypedEnvelope<proto::GitRemoveRemote>,
2700        mut cx: AsyncApp,
2701    ) -> Result<proto::Ack> {
2702        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2703        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2704        let remote_name = envelope.payload.remote_name;
2705
2706        repository_handle
2707            .update(&mut cx, |repository_handle, _| {
2708                repository_handle.remove_remote(remote_name)
2709            })
2710            .await??;
2711
2712        Ok(proto::Ack {})
2713    }
2714
2715    async fn handle_show(
2716        this: Entity<Self>,
2717        envelope: TypedEnvelope<proto::GitShow>,
2718        mut cx: AsyncApp,
2719    ) -> Result<proto::GitCommitDetails> {
2720        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2721        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2722
2723        let commit = repository_handle
2724            .update(&mut cx, |repository_handle, _| {
2725                repository_handle.show(envelope.payload.commit)
2726            })
2727            .await??;
2728        Ok(proto::GitCommitDetails {
2729            sha: commit.sha.into(),
2730            message: commit.message.into(),
2731            commit_timestamp: commit.commit_timestamp,
2732            author_email: commit.author_email.into(),
2733            author_name: commit.author_name.into(),
2734        })
2735    }
2736
2737    async fn handle_create_checkpoint(
2738        this: Entity<Self>,
2739        envelope: TypedEnvelope<proto::GitCreateCheckpoint>,
2740        mut cx: AsyncApp,
2741    ) -> Result<proto::GitCreateCheckpointResponse> {
2742        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2743        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2744
2745        let checkpoint = repository_handle
2746            .update(&mut cx, |repository, _| repository.checkpoint())
2747            .await??;
2748
2749        Ok(proto::GitCreateCheckpointResponse {
2750            commit_sha: checkpoint.commit_sha.as_bytes().to_vec(),
2751        })
2752    }
2753
2754    async fn handle_create_archive_checkpoint(
2755        this: Entity<Self>,
2756        envelope: TypedEnvelope<proto::GitCreateArchiveCheckpoint>,
2757        mut cx: AsyncApp,
2758    ) -> Result<proto::GitCreateArchiveCheckpointResponse> {
2759        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2760        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2761
2762        let (staged_commit_sha, unstaged_commit_sha) = repository_handle
2763            .update(&mut cx, |repository, _| {
2764                repository.create_archive_checkpoint()
2765            })
2766            .await??;
2767
2768        Ok(proto::GitCreateArchiveCheckpointResponse {
2769            staged_commit_sha,
2770            unstaged_commit_sha,
2771        })
2772    }
2773
2774    async fn handle_restore_checkpoint(
2775        this: Entity<Self>,
2776        envelope: TypedEnvelope<proto::GitRestoreCheckpoint>,
2777        mut cx: AsyncApp,
2778    ) -> Result<proto::Ack> {
2779        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2780        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2781
2782        let checkpoint = GitRepositoryCheckpoint {
2783            commit_sha: Oid::from_bytes(&envelope.payload.commit_sha)?,
2784        };
2785
2786        repository_handle
2787            .update(&mut cx, |repository, _| {
2788                repository.restore_checkpoint(checkpoint)
2789            })
2790            .await??;
2791
2792        Ok(proto::Ack {})
2793    }
2794
2795    async fn handle_restore_archive_checkpoint(
2796        this: Entity<Self>,
2797        envelope: TypedEnvelope<proto::GitRestoreArchiveCheckpoint>,
2798        mut cx: AsyncApp,
2799    ) -> Result<proto::Ack> {
2800        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2801        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2802        let staged_commit_sha = envelope.payload.staged_commit_sha;
2803        let unstaged_commit_sha = envelope.payload.unstaged_commit_sha;
2804
2805        repository_handle
2806            .update(&mut cx, |repository, _| {
2807                repository.restore_archive_checkpoint(staged_commit_sha, unstaged_commit_sha)
2808            })
2809            .await??;
2810
2811        Ok(proto::Ack {})
2812    }
2813
2814    async fn handle_compare_checkpoints(
2815        this: Entity<Self>,
2816        envelope: TypedEnvelope<proto::GitCompareCheckpoints>,
2817        mut cx: AsyncApp,
2818    ) -> Result<proto::GitCompareCheckpointsResponse> {
2819        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2820        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2821
2822        let left = GitRepositoryCheckpoint {
2823            commit_sha: Oid::from_bytes(&envelope.payload.left_commit_sha)?,
2824        };
2825        let right = GitRepositoryCheckpoint {
2826            commit_sha: Oid::from_bytes(&envelope.payload.right_commit_sha)?,
2827        };
2828
2829        let equal = repository_handle
2830            .update(&mut cx, |repository, _| {
2831                repository.compare_checkpoints(left, right)
2832            })
2833            .await??;
2834
2835        Ok(proto::GitCompareCheckpointsResponse { equal })
2836    }
2837
2838    async fn handle_diff_checkpoints(
2839        this: Entity<Self>,
2840        envelope: TypedEnvelope<proto::GitDiffCheckpoints>,
2841        mut cx: AsyncApp,
2842    ) -> Result<proto::GitDiffCheckpointsResponse> {
2843        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2844        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2845
2846        let base = GitRepositoryCheckpoint {
2847            commit_sha: Oid::from_bytes(&envelope.payload.base_commit_sha)?,
2848        };
2849        let target = GitRepositoryCheckpoint {
2850            commit_sha: Oid::from_bytes(&envelope.payload.target_commit_sha)?,
2851        };
2852
2853        let diff = repository_handle
2854            .update(&mut cx, |repository, _| {
2855                repository.diff_checkpoints(base, target)
2856            })
2857            .await??;
2858
2859        Ok(proto::GitDiffCheckpointsResponse { diff })
2860    }
2861
2862    async fn handle_load_commit_diff(
2863        this: Entity<Self>,
2864        envelope: TypedEnvelope<proto::LoadCommitDiff>,
2865        mut cx: AsyncApp,
2866    ) -> Result<proto::LoadCommitDiffResponse> {
2867        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2868        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2869
2870        let commit_diff = repository_handle
2871            .update(&mut cx, |repository_handle, _| {
2872                repository_handle.load_commit_diff(envelope.payload.commit)
2873            })
2874            .await??;
2875        Ok(proto::LoadCommitDiffResponse {
2876            files: commit_diff
2877                .files
2878                .into_iter()
2879                .map(|file| proto::CommitFile {
2880                    path: file.path.to_proto(),
2881                    old_text: file.old_text,
2882                    new_text: file.new_text,
2883                    is_binary: file.is_binary,
2884                })
2885                .collect(),
2886        })
2887    }
2888
2889    async fn handle_file_history(
2890        this: Entity<Self>,
2891        envelope: TypedEnvelope<proto::GitFileHistory>,
2892        mut cx: AsyncApp,
2893    ) -> Result<proto::GitFileHistoryResponse> {
2894        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2895        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2896        let path = RepoPath::from_proto(&envelope.payload.path)?;
2897        let skip = envelope.payload.skip as usize;
2898        let limit = envelope.payload.limit.map(|l| l as usize);
2899
2900        let file_history = repository_handle
2901            .update(&mut cx, |repository_handle, _| {
2902                repository_handle.file_history_paginated(path, skip, limit)
2903            })
2904            .await??;
2905
2906        Ok(proto::GitFileHistoryResponse {
2907            entries: file_history
2908                .entries
2909                .into_iter()
2910                .map(|entry| proto::FileHistoryEntry {
2911                    sha: entry.sha.to_string(),
2912                    subject: entry.subject.to_string(),
2913                    message: entry.message.to_string(),
2914                    commit_timestamp: entry.commit_timestamp,
2915                    author_name: entry.author_name.to_string(),
2916                    author_email: entry.author_email.to_string(),
2917                })
2918                .collect(),
2919            path: file_history.path.to_proto(),
2920        })
2921    }
2922
2923    async fn handle_reset(
2924        this: Entity<Self>,
2925        envelope: TypedEnvelope<proto::GitReset>,
2926        mut cx: AsyncApp,
2927    ) -> Result<proto::Ack> {
2928        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2929        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2930
2931        let mode = match envelope.payload.mode() {
2932            git_reset::ResetMode::Soft => ResetMode::Soft,
2933            git_reset::ResetMode::Mixed => ResetMode::Mixed,
2934        };
2935
2936        repository_handle
2937            .update(&mut cx, |repository_handle, cx| {
2938                repository_handle.reset(envelope.payload.commit, mode, cx)
2939            })
2940            .await??;
2941        Ok(proto::Ack {})
2942    }
2943
2944    async fn handle_checkout_files(
2945        this: Entity<Self>,
2946        envelope: TypedEnvelope<proto::GitCheckoutFiles>,
2947        mut cx: AsyncApp,
2948    ) -> Result<proto::Ack> {
2949        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2950        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
2951        let paths = envelope
2952            .payload
2953            .paths
2954            .iter()
2955            .map(|s| RepoPath::from_proto(s))
2956            .collect::<Result<Vec<_>>>()?;
2957
2958        repository_handle
2959            .update(&mut cx, |repository_handle, cx| {
2960                repository_handle.checkout_files(&envelope.payload.commit, paths, cx)
2961            })
2962            .await?;
2963        Ok(proto::Ack {})
2964    }
2965
2966    async fn handle_open_commit_message_buffer(
2967        this: Entity<Self>,
2968        envelope: TypedEnvelope<proto::OpenCommitMessageBuffer>,
2969        mut cx: AsyncApp,
2970    ) -> Result<proto::OpenBufferResponse> {
2971        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
2972        let repository = Self::repository_for_request(&this, repository_id, &mut cx)?;
2973        let buffer = repository
2974            .update(&mut cx, |repository, cx| {
2975                repository.open_commit_buffer(None, this.read(cx).buffer_store.clone(), cx)
2976            })
2977            .await?;
2978
2979        let buffer_id = buffer.read_with(&cx, |buffer, _| buffer.remote_id());
2980        this.update(&mut cx, |this, cx| {
2981            this.buffer_store.update(cx, |buffer_store, cx| {
2982                buffer_store
2983                    .create_buffer_for_peer(
2984                        &buffer,
2985                        envelope.original_sender_id.unwrap_or(envelope.sender_id),
2986                        cx,
2987                    )
2988                    .detach_and_log_err(cx);
2989            })
2990        });
2991
2992        Ok(proto::OpenBufferResponse {
2993            buffer_id: buffer_id.to_proto(),
2994        })
2995    }
2996
2997    async fn handle_askpass(
2998        this: Entity<Self>,
2999        envelope: TypedEnvelope<proto::AskPassRequest>,
3000        mut cx: AsyncApp,
3001    ) -> Result<proto::AskPassResponse> {
3002        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
3003        let repository = Self::repository_for_request(&this, repository_id, &mut cx)?;
3004
3005        let delegates = cx.update(|cx| repository.read(cx).askpass_delegates.clone());
3006        let Some(mut askpass) = delegates.lock().remove(&envelope.payload.askpass_id) else {
3007            debug_panic!("no askpass found");
3008            anyhow::bail!("no askpass found");
3009        };
3010
3011        let response = askpass
3012            .ask_password(envelope.payload.prompt)
3013            .await
3014            .ok_or_else(|| anyhow::anyhow!("askpass cancelled"))?;
3015
3016        delegates
3017            .lock()
3018            .insert(envelope.payload.askpass_id, askpass);
3019
3020        // In fact, we don't quite know what we're doing here, as we're sending askpass password unencrypted, but..
3021        Ok(proto::AskPassResponse {
3022            response: response.decrypt(IKnowWhatIAmDoingAndIHaveReadTheDocs)?,
3023        })
3024    }
3025
3026    async fn handle_check_for_pushed_commits(
3027        this: Entity<Self>,
3028        envelope: TypedEnvelope<proto::CheckForPushedCommits>,
3029        mut cx: AsyncApp,
3030    ) -> Result<proto::CheckForPushedCommitsResponse> {
3031        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
3032        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
3033
3034        let branches = repository_handle
3035            .update(&mut cx, |repository_handle, _| {
3036                repository_handle.check_for_pushed_commits()
3037            })
3038            .await??;
3039        Ok(proto::CheckForPushedCommitsResponse {
3040            pushed_to: branches
3041                .into_iter()
3042                .map(|commit| commit.to_string())
3043                .collect(),
3044        })
3045    }
3046
3047    async fn handle_git_diff(
3048        this: Entity<Self>,
3049        envelope: TypedEnvelope<proto::GitDiff>,
3050        mut cx: AsyncApp,
3051    ) -> Result<proto::GitDiffResponse> {
3052        let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
3053        let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
3054        let diff_type = match envelope.payload.diff_type() {
3055            proto::git_diff::DiffType::HeadToIndex => DiffType::HeadToIndex,
3056            proto::git_diff::DiffType::HeadToWorktree => DiffType::HeadToWorktree,
3057            proto::git_diff::DiffType::MergeBase => {
3058                let base_ref = envelope
3059                    .payload
3060                    .merge_base_ref
3061                    .ok_or_else(|| anyhow!("merge_base_ref is required for MergeBase diff type"))?;
3062                DiffType::MergeBase {
3063                    base_ref: base_ref.into(),
3064                }
3065            }
3066        };
3067
3068        let mut diff = repository_handle
3069            .update(&mut cx, |repository_handle, cx| {
3070                repository_handle.diff(diff_type, cx)
3071            })
3072            .await??;
3073        const ONE_MB: usize = 1_000_000;
3074        if diff.len() > ONE_MB {
3075            diff = diff.chars().take(ONE_MB).collect()
3076        }
3077
3078        Ok(proto::GitDiffResponse { diff })
3079    }
3080
3081    async fn handle_tree_diff(
3082        this: Entity<Self>,
3083        request: TypedEnvelope<proto::GetTreeDiff>,
3084        mut cx: AsyncApp,
3085    ) -> Result<proto::GetTreeDiffResponse> {
3086        let repository_id = RepositoryId(request.payload.repository_id);
3087        let diff_type = if request.payload.is_merge {
3088            DiffTreeType::MergeBase {
3089                base: request.payload.base.into(),
3090                head: request.payload.head.into(),
3091            }
3092        } else {
3093            DiffTreeType::Since {
3094                base: request.payload.base.into(),
3095                head: request.payload.head.into(),
3096            }
3097        };
3098
3099        let diff = this
3100            .update(&mut cx, |this, cx| {
3101                let repository = this.repositories().get(&repository_id)?;
3102                Some(repository.update(cx, |repo, cx| repo.diff_tree(diff_type, cx)))
3103            })
3104            .context("missing repository")?
3105            .await??;
3106
3107        Ok(proto::GetTreeDiffResponse {
3108            entries: diff
3109                .entries
3110                .into_iter()
3111                .map(|(path, status)| proto::TreeDiffStatus {
3112                    path: path.as_ref().to_proto(),
3113                    status: match status {
3114                        TreeDiffStatus::Added {} => proto::tree_diff_status::Status::Added.into(),
3115                        TreeDiffStatus::Modified { .. } => {
3116                            proto::tree_diff_status::Status::Modified.into()
3117                        }
3118                        TreeDiffStatus::Deleted { .. } => {
3119                            proto::tree_diff_status::Status::Deleted.into()
3120                        }
3121                    },
3122                    oid: match status {
3123                        TreeDiffStatus::Deleted { old } | TreeDiffStatus::Modified { old } => {
3124                            Some(old.to_string())
3125                        }
3126                        TreeDiffStatus::Added => None,
3127                    },
3128                })
3129                .collect(),
3130        })
3131    }
3132
3133    async fn handle_get_blob_content(
3134        this: Entity<Self>,
3135        request: TypedEnvelope<proto::GetBlobContent>,
3136        mut cx: AsyncApp,
3137    ) -> Result<proto::GetBlobContentResponse> {
3138        let oid = git::Oid::from_str(&request.payload.oid)?;
3139        let repository_id = RepositoryId(request.payload.repository_id);
3140        let content = this
3141            .update(&mut cx, |this, cx| {
3142                let repository = this.repositories().get(&repository_id)?;
3143                Some(repository.update(cx, |repo, cx| repo.load_blob_content(oid, cx)))
3144            })
3145            .context("missing repository")?
3146            .await?;
3147        Ok(proto::GetBlobContentResponse { content })
3148    }
3149
3150    async fn handle_open_unstaged_diff(
3151        this: Entity<Self>,
3152        request: TypedEnvelope<proto::OpenUnstagedDiff>,
3153        mut cx: AsyncApp,
3154    ) -> Result<proto::OpenUnstagedDiffResponse> {
3155        let buffer_id = BufferId::new(request.payload.buffer_id)?;
3156        let diff = this
3157            .update(&mut cx, |this, cx| {
3158                let buffer = this.buffer_store.read(cx).get(buffer_id)?;
3159                Some(this.open_unstaged_diff(buffer, cx))
3160            })
3161            .context("missing buffer")?
3162            .await?;
3163        this.update(&mut cx, |this, _| {
3164            let shared_diffs = this
3165                .shared_diffs
3166                .entry(request.original_sender_id.unwrap_or(request.sender_id))
3167                .or_default();
3168            shared_diffs.entry(buffer_id).or_default().unstaged = Some(diff.clone());
3169        });
3170        let staged_text = diff.read_with(&cx, |diff, cx| diff.base_text_string(cx));
3171        Ok(proto::OpenUnstagedDiffResponse { staged_text })
3172    }
3173
3174    async fn handle_open_uncommitted_diff(
3175        this: Entity<Self>,
3176        request: TypedEnvelope<proto::OpenUncommittedDiff>,
3177        mut cx: AsyncApp,
3178    ) -> Result<proto::OpenUncommittedDiffResponse> {
3179        let buffer_id = BufferId::new(request.payload.buffer_id)?;
3180        let diff = this
3181            .update(&mut cx, |this, cx| {
3182                let buffer = this.buffer_store.read(cx).get(buffer_id)?;
3183                Some(this.open_uncommitted_diff(buffer, cx))
3184            })
3185            .context("missing buffer")?
3186            .await?;
3187        this.update(&mut cx, |this, _| {
3188            let shared_diffs = this
3189                .shared_diffs
3190                .entry(request.original_sender_id.unwrap_or(request.sender_id))
3191                .or_default();
3192            shared_diffs.entry(buffer_id).or_default().uncommitted = Some(diff.clone());
3193        });
3194        Ok(diff.read_with(&cx, |diff, cx| {
3195            use proto::open_uncommitted_diff_response::Mode;
3196
3197            let unstaged_diff = diff.secondary_diff();
3198            let index_snapshot = unstaged_diff.and_then(|diff| {
3199                let diff = diff.read(cx);
3200                diff.base_text_exists().then(|| diff.base_text(cx))
3201            });
3202
3203            let mode;
3204            let staged_text;
3205            let committed_text;
3206            if diff.base_text_exists() {
3207                let committed_snapshot = diff.base_text(cx);
3208                committed_text = Some(committed_snapshot.text());
3209                if let Some(index_text) = index_snapshot {
3210                    if index_text.remote_id() == committed_snapshot.remote_id() {
3211                        mode = Mode::IndexMatchesHead;
3212                        staged_text = None;
3213                    } else {
3214                        mode = Mode::IndexAndHead;
3215                        staged_text = Some(index_text.text());
3216                    }
3217                } else {
3218                    mode = Mode::IndexAndHead;
3219                    staged_text = None;
3220                }
3221            } else {
3222                mode = Mode::IndexAndHead;
3223                committed_text = None;
3224                staged_text = index_snapshot.as_ref().map(|buffer| buffer.text());
3225            }
3226
3227            proto::OpenUncommittedDiffResponse {
3228                committed_text,
3229                staged_text,
3230                mode: mode.into(),
3231            }
3232        }))
3233    }
3234
3235    async fn handle_update_diff_bases(
3236        this: Entity<Self>,
3237        request: TypedEnvelope<proto::UpdateDiffBases>,
3238        mut cx: AsyncApp,
3239    ) -> Result<()> {
3240        let buffer_id = BufferId::new(request.payload.buffer_id)?;
3241        this.update(&mut cx, |this, cx| {
3242            if let Some(diff_state) = this.diffs.get_mut(&buffer_id)
3243                && let Some(buffer) = this.buffer_store.read(cx).get(buffer_id)
3244            {
3245                let buffer = buffer.read(cx).text_snapshot();
3246                diff_state.update(cx, |diff_state, cx| {
3247                    diff_state.handle_base_texts_updated(buffer, request.payload, cx);
3248                })
3249            }
3250        });
3251        Ok(())
3252    }
3253
3254    async fn handle_blame_buffer(
3255        this: Entity<Self>,
3256        envelope: TypedEnvelope<proto::BlameBuffer>,
3257        mut cx: AsyncApp,
3258    ) -> Result<proto::BlameBufferResponse> {
3259        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
3260        let version = deserialize_version(&envelope.payload.version);
3261        let buffer = this.read_with(&cx, |this, cx| {
3262            this.buffer_store.read(cx).get_existing(buffer_id)
3263        })?;
3264        buffer
3265            .update(&mut cx, |buffer, _| {
3266                buffer.wait_for_version(version.clone())
3267            })
3268            .await?;
3269        let blame = this
3270            .update(&mut cx, |this, cx| {
3271                this.blame_buffer(&buffer, Some(version), cx)
3272            })
3273            .await?;
3274        Ok(serialize_blame_buffer_response(blame))
3275    }
3276
3277    async fn handle_get_permalink_to_line(
3278        this: Entity<Self>,
3279        envelope: TypedEnvelope<proto::GetPermalinkToLine>,
3280        mut cx: AsyncApp,
3281    ) -> Result<proto::GetPermalinkToLineResponse> {
3282        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
3283        // let version = deserialize_version(&envelope.payload.version);
3284        let selection = {
3285            let proto_selection = envelope
3286                .payload
3287                .selection
3288                .context("no selection to get permalink for defined")?;
3289            proto_selection.start as u32..proto_selection.end as u32
3290        };
3291        let buffer = this.read_with(&cx, |this, cx| {
3292            this.buffer_store.read(cx).get_existing(buffer_id)
3293        })?;
3294        let permalink = this
3295            .update(&mut cx, |this, cx| {
3296                this.get_permalink_to_line(&buffer, selection, cx)
3297            })
3298            .await?;
3299        Ok(proto::GetPermalinkToLineResponse {
3300            permalink: permalink.to_string(),
3301        })
3302    }
3303
3304    fn repository_for_request(
3305        this: &Entity<Self>,
3306        id: RepositoryId,
3307        cx: &mut AsyncApp,
3308    ) -> Result<Entity<Repository>> {
3309        this.read_with(cx, |this, _| {
3310            this.repositories
3311                .get(&id)
3312                .context("missing repository handle")
3313                .cloned()
3314        })
3315    }
3316
3317    pub fn repo_snapshots(&self, cx: &App) -> HashMap<RepositoryId, RepositorySnapshot> {
3318        self.repositories
3319            .iter()
3320            .map(|(id, repo)| (*id, repo.read(cx).snapshot.clone()))
3321            .collect()
3322    }
3323
3324    fn process_updated_entries(
3325        &self,
3326        worktree: &Entity<Worktree>,
3327        updated_entries: &[(Arc<RelPath>, ProjectEntryId, PathChange)],
3328        cx: &mut App,
3329    ) -> Task<HashMap<Entity<Repository>, Vec<RepoPath>>> {
3330        let path_style = worktree.read(cx).path_style();
3331        let mut repo_paths = self
3332            .repositories
3333            .values()
3334            .map(|repo| (repo.read(cx).work_directory_abs_path.clone(), repo.clone()))
3335            .collect::<Vec<_>>();
3336        let mut entries: Vec<_> = updated_entries
3337            .iter()
3338            .map(|(path, _, _)| path.clone())
3339            .collect();
3340        entries.sort();
3341        let worktree = worktree.read(cx);
3342
3343        let entries = entries
3344            .into_iter()
3345            .map(|path| worktree.absolutize(&path))
3346            .collect::<Arc<[_]>>();
3347
3348        let executor = cx.background_executor().clone();
3349        cx.background_executor().spawn(async move {
3350            repo_paths.sort_by(|lhs, rhs| lhs.0.cmp(&rhs.0));
3351            let mut paths_by_git_repo = HashMap::<_, Vec<_>>::default();
3352            let mut tasks = FuturesOrdered::new();
3353            for (repo_path, repo) in repo_paths.into_iter().rev() {
3354                let entries = entries.clone();
3355                let task = executor.spawn(async move {
3356                    // Find all repository paths that belong to this repo
3357                    let mut ix = entries.partition_point(|path| path < &*repo_path);
3358                    if ix == entries.len() {
3359                        return None;
3360                    };
3361
3362                    let mut paths = Vec::new();
3363                    // All paths prefixed by a given repo will constitute a continuous range.
3364                    while let Some(path) = entries.get(ix)
3365                        && let Some(repo_path) = RepositorySnapshot::abs_path_to_repo_path_inner(
3366                            &repo_path, path, path_style,
3367                        )
3368                    {
3369                        paths.push((repo_path, ix));
3370                        ix += 1;
3371                    }
3372                    if paths.is_empty() {
3373                        None
3374                    } else {
3375                        Some((repo, paths))
3376                    }
3377                });
3378                tasks.push_back(task);
3379            }
3380
3381            // Now, let's filter out the "duplicate" entries that were processed by multiple distinct repos.
3382            let mut path_was_used = vec![false; entries.len()];
3383            let tasks = tasks.collect::<Vec<_>>().await;
3384            // Process tasks from the back: iterating backwards allows us to see more-specific paths first.
3385            // We always want to assign a path to it's innermost repository.
3386            for t in tasks {
3387                let Some((repo, paths)) = t else {
3388                    continue;
3389                };
3390                let entry = paths_by_git_repo.entry(repo).or_default();
3391                for (repo_path, ix) in paths {
3392                    if path_was_used[ix] {
3393                        continue;
3394                    }
3395                    path_was_used[ix] = true;
3396                    entry.push(repo_path);
3397                }
3398            }
3399
3400            paths_by_git_repo
3401        })
3402    }
3403}
3404
3405impl BufferGitState {
3406    fn new(_git_store: WeakEntity<GitStore>) -> Self {
3407        Self {
3408            unstaged_diff: Default::default(),
3409            uncommitted_diff: Default::default(),
3410            oid_diffs: Default::default(),
3411            recalculate_diff_task: Default::default(),
3412            language: Default::default(),
3413            language_registry: Default::default(),
3414            recalculating_tx: postage::watch::channel_with(false).0,
3415            hunk_staging_operation_count: 0,
3416            hunk_staging_operation_count_as_of_write: 0,
3417            head_text: Default::default(),
3418            index_text: Default::default(),
3419            oid_texts: Default::default(),
3420            head_changed: Default::default(),
3421            index_changed: Default::default(),
3422            language_changed: Default::default(),
3423            conflict_updated_futures: Default::default(),
3424            conflict_set: Default::default(),
3425            reparse_conflict_markers_task: Default::default(),
3426        }
3427    }
3428
3429    #[ztracing::instrument(skip_all)]
3430    fn buffer_language_changed(&mut self, buffer: Entity<Buffer>, cx: &mut Context<Self>) {
3431        self.language = buffer.read(cx).language().cloned();
3432        self.language_changed = true;
3433        let _ = self.recalculate_diffs(buffer.read(cx).text_snapshot(), cx);
3434    }
3435
3436    fn reparse_conflict_markers(
3437        &mut self,
3438        buffer: text::BufferSnapshot,
3439        cx: &mut Context<Self>,
3440    ) -> oneshot::Receiver<()> {
3441        let (tx, rx) = oneshot::channel();
3442
3443        let Some(conflict_set) = self
3444            .conflict_set
3445            .as_ref()
3446            .and_then(|conflict_set| conflict_set.upgrade())
3447        else {
3448            return rx;
3449        };
3450
3451        let old_snapshot = conflict_set.read_with(cx, |conflict_set, _| {
3452            if conflict_set.has_conflict {
3453                Some(conflict_set.snapshot())
3454            } else {
3455                None
3456            }
3457        });
3458
3459        if let Some(old_snapshot) = old_snapshot {
3460            self.conflict_updated_futures.push(tx);
3461            self.reparse_conflict_markers_task = Some(cx.spawn(async move |this, cx| {
3462                let (snapshot, changed_range) = cx
3463                    .background_spawn(async move {
3464                        let new_snapshot = ConflictSet::parse(&buffer);
3465                        let changed_range = old_snapshot.compare(&new_snapshot, &buffer);
3466                        (new_snapshot, changed_range)
3467                    })
3468                    .await;
3469                this.update(cx, |this, cx| {
3470                    if let Some(conflict_set) = &this.conflict_set {
3471                        conflict_set
3472                            .update(cx, |conflict_set, cx| {
3473                                conflict_set.set_snapshot(snapshot, changed_range, cx);
3474                            })
3475                            .ok();
3476                    }
3477                    let futures = std::mem::take(&mut this.conflict_updated_futures);
3478                    for tx in futures {
3479                        tx.send(()).ok();
3480                    }
3481                })
3482            }))
3483        }
3484
3485        rx
3486    }
3487
3488    fn unstaged_diff(&self) -> Option<Entity<BufferDiff>> {
3489        self.unstaged_diff.as_ref().and_then(|set| set.upgrade())
3490    }
3491
3492    fn uncommitted_diff(&self) -> Option<Entity<BufferDiff>> {
3493        self.uncommitted_diff.as_ref().and_then(|set| set.upgrade())
3494    }
3495
3496    fn oid_diff(&self, oid: Option<git::Oid>) -> Option<Entity<BufferDiff>> {
3497        self.oid_diffs.get(&oid).and_then(|weak| weak.upgrade())
3498    }
3499
3500    fn handle_base_texts_updated(
3501        &mut self,
3502        buffer: text::BufferSnapshot,
3503        message: proto::UpdateDiffBases,
3504        cx: &mut Context<Self>,
3505    ) {
3506        use proto::update_diff_bases::Mode;
3507
3508        let Some(mode) = Mode::from_i32(message.mode) else {
3509            return;
3510        };
3511
3512        let diff_bases_change = match mode {
3513            Mode::HeadOnly => DiffBasesChange::SetHead(message.committed_text),
3514            Mode::IndexOnly => DiffBasesChange::SetIndex(message.staged_text),
3515            Mode::IndexMatchesHead => DiffBasesChange::SetBoth(message.committed_text),
3516            Mode::IndexAndHead => DiffBasesChange::SetEach {
3517                index: message.staged_text,
3518                head: message.committed_text,
3519            },
3520        };
3521
3522        self.diff_bases_changed(buffer, Some(diff_bases_change), cx);
3523    }
3524
3525    pub fn wait_for_recalculation(&mut self) -> Option<impl Future<Output = ()> + use<>> {
3526        if *self.recalculating_tx.borrow() {
3527            let mut rx = self.recalculating_tx.subscribe();
3528            Some(async move {
3529                loop {
3530                    let is_recalculating = rx.recv().await;
3531                    if is_recalculating != Some(true) {
3532                        break;
3533                    }
3534                }
3535            })
3536        } else {
3537            None
3538        }
3539    }
3540
3541    fn diff_bases_changed(
3542        &mut self,
3543        buffer: text::BufferSnapshot,
3544        diff_bases_change: Option<DiffBasesChange>,
3545        cx: &mut Context<Self>,
3546    ) {
3547        match diff_bases_change {
3548            Some(DiffBasesChange::SetIndex(index)) => {
3549                self.index_text = index.map(|mut index| {
3550                    text::LineEnding::normalize(&mut index);
3551                    Arc::from(index.as_str())
3552                });
3553                self.index_changed = true;
3554            }
3555            Some(DiffBasesChange::SetHead(head)) => {
3556                self.head_text = head.map(|mut head| {
3557                    text::LineEnding::normalize(&mut head);
3558                    Arc::from(head.as_str())
3559                });
3560                self.head_changed = true;
3561            }
3562            Some(DiffBasesChange::SetBoth(text)) => {
3563                let text = text.map(|mut text| {
3564                    text::LineEnding::normalize(&mut text);
3565                    Arc::from(text.as_str())
3566                });
3567                self.head_text = text.clone();
3568                self.index_text = text;
3569                self.head_changed = true;
3570                self.index_changed = true;
3571            }
3572            Some(DiffBasesChange::SetEach { index, head }) => {
3573                self.index_text = index.map(|mut index| {
3574                    text::LineEnding::normalize(&mut index);
3575                    Arc::from(index.as_str())
3576                });
3577                self.index_changed = true;
3578                self.head_text = head.map(|mut head| {
3579                    text::LineEnding::normalize(&mut head);
3580                    Arc::from(head.as_str())
3581                });
3582                self.head_changed = true;
3583            }
3584            None => {}
3585        }
3586
3587        self.recalculate_diffs(buffer, cx)
3588    }
3589
3590    #[ztracing::instrument(skip_all)]
3591    fn recalculate_diffs(&mut self, buffer: text::BufferSnapshot, cx: &mut Context<Self>) {
3592        *self.recalculating_tx.borrow_mut() = true;
3593
3594        let language = self.language.clone();
3595        let language_registry = self.language_registry.clone();
3596        let unstaged_diff = self.unstaged_diff();
3597        let uncommitted_diff = self.uncommitted_diff();
3598        let head = self.head_text.clone();
3599        let index = self.index_text.clone();
3600        let index_changed = self.index_changed;
3601        let head_changed = self.head_changed;
3602        let language_changed = self.language_changed;
3603        let prev_hunk_staging_operation_count = self.hunk_staging_operation_count_as_of_write;
3604        let index_matches_head = match (self.index_text.as_ref(), self.head_text.as_ref()) {
3605            (Some(index), Some(head)) => Arc::ptr_eq(index, head),
3606            (None, None) => true,
3607            _ => false,
3608        };
3609
3610        let oid_diffs: Vec<(Option<git::Oid>, Entity<BufferDiff>, Option<Arc<str>>)> = self
3611            .oid_diffs
3612            .iter()
3613            .filter_map(|(oid, weak)| {
3614                let base_text = oid.and_then(|oid| self.oid_texts.get(&oid).cloned());
3615                weak.upgrade().map(|diff| (*oid, diff, base_text))
3616            })
3617            .collect();
3618
3619        self.oid_diffs.retain(|oid, weak| {
3620            let alive = weak.upgrade().is_some();
3621            if !alive {
3622                if let Some(oid) = oid {
3623                    self.oid_texts.remove(oid);
3624                }
3625            }
3626            alive
3627        });
3628        self.recalculate_diff_task = Some(cx.spawn(async move |this, cx| {
3629            log::debug!(
3630                "start recalculating diffs for buffer {}",
3631                buffer.remote_id()
3632            );
3633
3634            let mut new_unstaged_diff = None;
3635            if let Some(unstaged_diff) = &unstaged_diff {
3636                new_unstaged_diff = Some(
3637                    cx.update(|cx| {
3638                        unstaged_diff.read(cx).update_diff(
3639                            buffer.clone(),
3640                            index,
3641                            index_changed.then_some(false),
3642                            language.clone(),
3643                            cx,
3644                        )
3645                    })
3646                    .await,
3647                );
3648            }
3649
3650            // Dropping BufferDiff can be expensive, so yield back to the event loop
3651            // for a bit
3652            yield_now().await;
3653
3654            let mut new_uncommitted_diff = None;
3655            if let Some(uncommitted_diff) = &uncommitted_diff {
3656                new_uncommitted_diff = if index_matches_head {
3657                    new_unstaged_diff.clone()
3658                } else {
3659                    Some(
3660                        cx.update(|cx| {
3661                            uncommitted_diff.read(cx).update_diff(
3662                                buffer.clone(),
3663                                head,
3664                                head_changed.then_some(true),
3665                                language.clone(),
3666                                cx,
3667                            )
3668                        })
3669                        .await,
3670                    )
3671                }
3672            }
3673
3674            // Dropping BufferDiff can be expensive, so yield back to the event loop
3675            // for a bit
3676            yield_now().await;
3677
3678            let cancel = this.update(cx, |this, _| {
3679                // This checks whether all pending stage/unstage operations
3680                // have quiesced (i.e. both the corresponding write and the
3681                // read of that write have completed). If not, then we cancel
3682                // this recalculation attempt to avoid invalidating pending
3683                // state too quickly; another recalculation will come along
3684                // later and clear the pending state once the state of the index has settled.
3685                if this.hunk_staging_operation_count > prev_hunk_staging_operation_count {
3686                    *this.recalculating_tx.borrow_mut() = false;
3687                    true
3688                } else {
3689                    false
3690                }
3691            })?;
3692            if cancel {
3693                log::debug!(
3694                    concat!(
3695                        "aborting recalculating diffs for buffer {}",
3696                        "due to subsequent hunk operations",
3697                    ),
3698                    buffer.remote_id()
3699                );
3700                return Ok(());
3701            }
3702
3703            let unstaged_changed_range = if let Some((unstaged_diff, new_unstaged_diff)) =
3704                unstaged_diff.as_ref().zip(new_unstaged_diff.clone())
3705            {
3706                let task = unstaged_diff.update(cx, |diff, cx| {
3707                    // For git index buffer we skip assigning the language as we do not really need to perform any syntax highlighting on
3708                    // it. As a result, by skipping it we are potentially shaving off a lot of RSS plus we get a snappier feel for large diff
3709                    // view multibuffers.
3710                    diff.set_snapshot(new_unstaged_diff, &buffer, cx)
3711                });
3712                Some(task.await)
3713            } else {
3714                None
3715            };
3716
3717            yield_now().await;
3718
3719            if let Some((uncommitted_diff, new_uncommitted_diff)) =
3720                uncommitted_diff.as_ref().zip(new_uncommitted_diff.clone())
3721            {
3722                uncommitted_diff
3723                    .update(cx, |diff, cx| {
3724                        if language_changed {
3725                            diff.language_changed(language.clone(), language_registry.clone(), cx);
3726                        }
3727                        diff.set_snapshot_with_secondary(
3728                            new_uncommitted_diff,
3729                            &buffer,
3730                            unstaged_changed_range.flatten(),
3731                            true,
3732                            cx,
3733                        )
3734                    })
3735                    .await;
3736            }
3737
3738            yield_now().await;
3739
3740            for (oid, oid_diff, base_text) in oid_diffs {
3741                let new_oid_diff = cx
3742                    .update(|cx| {
3743                        oid_diff.read(cx).update_diff(
3744                            buffer.clone(),
3745                            base_text,
3746                            None,
3747                            language.clone(),
3748                            cx,
3749                        )
3750                    })
3751                    .await;
3752
3753                oid_diff
3754                    .update(cx, |diff, cx| {
3755                        if language_changed {
3756                            diff.language_changed(language.clone(), language_registry.clone(), cx);
3757                        }
3758                        diff.set_snapshot(new_oid_diff, &buffer, cx)
3759                    })
3760                    .await;
3761
3762                log::debug!(
3763                    "finished recalculating oid diff for buffer {} oid {:?}",
3764                    buffer.remote_id(),
3765                    oid
3766                );
3767
3768                yield_now().await;
3769            }
3770
3771            log::debug!(
3772                "finished recalculating diffs for buffer {}",
3773                buffer.remote_id()
3774            );
3775
3776            if let Some(this) = this.upgrade() {
3777                this.update(cx, |this, _| {
3778                    this.index_changed = false;
3779                    this.head_changed = false;
3780                    this.language_changed = false;
3781                    *this.recalculating_tx.borrow_mut() = false;
3782                });
3783            }
3784
3785            Ok(())
3786        }));
3787    }
3788}
3789
3790fn make_remote_delegate(
3791    this: Entity<GitStore>,
3792    project_id: u64,
3793    repository_id: RepositoryId,
3794    askpass_id: u64,
3795    cx: &mut AsyncApp,
3796) -> AskPassDelegate {
3797    AskPassDelegate::new(cx, move |prompt, tx, cx| {
3798        this.update(cx, |this, cx| {
3799            let Some((client, _)) = this.downstream_client() else {
3800                return;
3801            };
3802            let response = client.request(proto::AskPassRequest {
3803                project_id,
3804                repository_id: repository_id.to_proto(),
3805                askpass_id,
3806                prompt,
3807            });
3808            cx.spawn(async move |_, _| {
3809                let mut response = response.await?.response;
3810                tx.send(EncryptedPassword::try_from(response.as_ref())?)
3811                    .ok();
3812                response.zeroize();
3813                anyhow::Ok(())
3814            })
3815            .detach_and_log_err(cx);
3816        });
3817    })
3818}
3819
3820impl RepositoryId {
3821    pub fn to_proto(self) -> u64 {
3822        self.0
3823    }
3824
3825    pub fn from_proto(id: u64) -> Self {
3826        RepositoryId(id)
3827    }
3828}
3829
3830impl RepositorySnapshot {
3831    fn empty(
3832        id: RepositoryId,
3833        work_directory_abs_path: Arc<Path>,
3834        original_repo_abs_path: Option<Arc<Path>>,
3835        path_style: PathStyle,
3836    ) -> Self {
3837        Self {
3838            id,
3839            statuses_by_path: Default::default(),
3840            original_repo_abs_path: original_repo_abs_path
3841                .unwrap_or_else(|| work_directory_abs_path.clone()),
3842            work_directory_abs_path,
3843            branch: None,
3844            branch_list: Arc::from([]),
3845            head_commit: None,
3846            scan_id: 0,
3847            merge: Default::default(),
3848            remote_origin_url: None,
3849            remote_upstream_url: None,
3850            stash_entries: Default::default(),
3851            linked_worktrees: Arc::from([]),
3852            path_style,
3853        }
3854    }
3855
3856    fn initial_update(&self, project_id: u64) -> proto::UpdateRepository {
3857        proto::UpdateRepository {
3858            branch_summary: self.branch.as_ref().map(branch_to_proto),
3859            head_commit_details: self.head_commit.as_ref().map(commit_details_to_proto),
3860            updated_statuses: self
3861                .statuses_by_path
3862                .iter()
3863                .map(|entry| entry.to_proto())
3864                .collect(),
3865            removed_statuses: Default::default(),
3866            current_merge_conflicts: self
3867                .merge
3868                .merge_heads_by_conflicted_path
3869                .iter()
3870                .map(|(repo_path, _)| repo_path.to_proto())
3871                .collect(),
3872            merge_message: self.merge.message.as_ref().map(|msg| msg.to_string()),
3873            project_id,
3874            id: self.id.to_proto(),
3875            abs_path: self.work_directory_abs_path.to_string_lossy().into_owned(),
3876            entry_ids: vec![self.id.to_proto()],
3877            scan_id: self.scan_id,
3878            is_last_update: true,
3879            stash_entries: self
3880                .stash_entries
3881                .entries
3882                .iter()
3883                .map(stash_to_proto)
3884                .collect(),
3885            remote_upstream_url: self.remote_upstream_url.clone(),
3886            remote_origin_url: self.remote_origin_url.clone(),
3887            original_repo_abs_path: Some(
3888                self.original_repo_abs_path.to_string_lossy().into_owned(),
3889            ),
3890            linked_worktrees: self
3891                .linked_worktrees
3892                .iter()
3893                .map(worktree_to_proto)
3894                .collect(),
3895        }
3896    }
3897
3898    fn build_update(&self, old: &Self, project_id: u64) -> proto::UpdateRepository {
3899        let mut updated_statuses: Vec<proto::StatusEntry> = Vec::new();
3900        let mut removed_statuses: Vec<String> = Vec::new();
3901
3902        let mut new_statuses = self.statuses_by_path.iter().peekable();
3903        let mut old_statuses = old.statuses_by_path.iter().peekable();
3904
3905        let mut current_new_entry = new_statuses.next();
3906        let mut current_old_entry = old_statuses.next();
3907        loop {
3908            match (current_new_entry, current_old_entry) {
3909                (Some(new_entry), Some(old_entry)) => {
3910                    match new_entry.repo_path.cmp(&old_entry.repo_path) {
3911                        Ordering::Less => {
3912                            updated_statuses.push(new_entry.to_proto());
3913                            current_new_entry = new_statuses.next();
3914                        }
3915                        Ordering::Equal => {
3916                            if new_entry.status != old_entry.status
3917                                || new_entry.diff_stat != old_entry.diff_stat
3918                            {
3919                                updated_statuses.push(new_entry.to_proto());
3920                            }
3921                            current_old_entry = old_statuses.next();
3922                            current_new_entry = new_statuses.next();
3923                        }
3924                        Ordering::Greater => {
3925                            removed_statuses.push(old_entry.repo_path.to_proto());
3926                            current_old_entry = old_statuses.next();
3927                        }
3928                    }
3929                }
3930                (None, Some(old_entry)) => {
3931                    removed_statuses.push(old_entry.repo_path.to_proto());
3932                    current_old_entry = old_statuses.next();
3933                }
3934                (Some(new_entry), None) => {
3935                    updated_statuses.push(new_entry.to_proto());
3936                    current_new_entry = new_statuses.next();
3937                }
3938                (None, None) => break,
3939            }
3940        }
3941
3942        proto::UpdateRepository {
3943            branch_summary: self.branch.as_ref().map(branch_to_proto),
3944            head_commit_details: self.head_commit.as_ref().map(commit_details_to_proto),
3945            updated_statuses,
3946            removed_statuses,
3947            current_merge_conflicts: self
3948                .merge
3949                .merge_heads_by_conflicted_path
3950                .iter()
3951                .map(|(path, _)| path.to_proto())
3952                .collect(),
3953            merge_message: self.merge.message.as_ref().map(|msg| msg.to_string()),
3954            project_id,
3955            id: self.id.to_proto(),
3956            abs_path: self.work_directory_abs_path.to_string_lossy().into_owned(),
3957            entry_ids: vec![],
3958            scan_id: self.scan_id,
3959            is_last_update: true,
3960            stash_entries: self
3961                .stash_entries
3962                .entries
3963                .iter()
3964                .map(stash_to_proto)
3965                .collect(),
3966            remote_upstream_url: self.remote_upstream_url.clone(),
3967            remote_origin_url: self.remote_origin_url.clone(),
3968            original_repo_abs_path: Some(
3969                self.original_repo_abs_path.to_string_lossy().into_owned(),
3970            ),
3971            linked_worktrees: self
3972                .linked_worktrees
3973                .iter()
3974                .map(worktree_to_proto)
3975                .collect(),
3976        }
3977    }
3978
3979    /// The main worktree is the original checkout that other worktrees were
3980    /// created from.
3981    ///
3982    /// For example, if you had both `~/code/zed` and `~/code/worktrees/zed-2`,
3983    /// then `~/code/zed` is the main worktree and `~/code/worktrees/zed-2` is a linked worktree.
3984    ///
3985    /// Submodules also return `true` here, since they are not linked worktrees.
3986    pub fn is_main_worktree(&self) -> bool {
3987        self.work_directory_abs_path == self.original_repo_abs_path
3988    }
3989
3990    /// Returns true if this repository is a linked worktree, that is, one that
3991    /// was created from another worktree.
3992    ///
3993    /// Returns `false` for both the main worktree and submodules.
3994    pub fn is_linked_worktree(&self) -> bool {
3995        !self.is_main_worktree()
3996    }
3997
3998    pub fn linked_worktrees(&self) -> &[GitWorktree] {
3999        &self.linked_worktrees
4000    }
4001
4002    pub fn status(&self) -> impl Iterator<Item = StatusEntry> + '_ {
4003        self.statuses_by_path.iter().cloned()
4004    }
4005
4006    pub fn status_summary(&self) -> GitSummary {
4007        self.statuses_by_path.summary().item_summary
4008    }
4009
4010    pub fn status_for_path(&self, path: &RepoPath) -> Option<StatusEntry> {
4011        self.statuses_by_path
4012            .get(&PathKey(path.as_ref().clone()), ())
4013            .cloned()
4014    }
4015
4016    pub fn diff_stat_for_path(&self, path: &RepoPath) -> Option<DiffStat> {
4017        self.statuses_by_path
4018            .get(&PathKey(path.as_ref().clone()), ())
4019            .and_then(|entry| entry.diff_stat)
4020    }
4021
4022    pub fn abs_path_to_repo_path(&self, abs_path: &Path) -> Option<RepoPath> {
4023        Self::abs_path_to_repo_path_inner(&self.work_directory_abs_path, abs_path, self.path_style)
4024    }
4025
4026    fn repo_path_to_abs_path(&self, repo_path: &RepoPath) -> PathBuf {
4027        self.path_style
4028            .join(&self.work_directory_abs_path, repo_path.as_std_path())
4029            .unwrap()
4030            .into()
4031    }
4032
4033    #[inline]
4034    fn abs_path_to_repo_path_inner(
4035        work_directory_abs_path: &Path,
4036        abs_path: &Path,
4037        path_style: PathStyle,
4038    ) -> Option<RepoPath> {
4039        let rel_path = path_style.strip_prefix(abs_path, work_directory_abs_path)?;
4040        Some(RepoPath::from_rel_path(&rel_path))
4041    }
4042
4043    pub fn had_conflict_on_last_merge_head_change(&self, repo_path: &RepoPath) -> bool {
4044        self.merge
4045            .merge_heads_by_conflicted_path
4046            .contains_key(repo_path)
4047    }
4048
4049    pub fn has_conflict(&self, repo_path: &RepoPath) -> bool {
4050        let had_conflict_on_last_merge_head_change = self
4051            .merge
4052            .merge_heads_by_conflicted_path
4053            .contains_key(repo_path);
4054        let has_conflict_currently = self
4055            .status_for_path(repo_path)
4056            .is_some_and(|entry| entry.status.is_conflicted());
4057        had_conflict_on_last_merge_head_change || has_conflict_currently
4058    }
4059
4060    /// This is the name that will be displayed in the repository selector for this repository.
4061    pub fn display_name(&self) -> SharedString {
4062        self.work_directory_abs_path
4063            .file_name()
4064            .unwrap_or_default()
4065            .to_string_lossy()
4066            .to_string()
4067            .into()
4068    }
4069}
4070
4071pub fn stash_to_proto(entry: &StashEntry) -> proto::StashEntry {
4072    proto::StashEntry {
4073        oid: entry.oid.as_bytes().to_vec(),
4074        message: entry.message.clone(),
4075        branch: entry.branch.clone(),
4076        index: entry.index as u64,
4077        timestamp: entry.timestamp,
4078    }
4079}
4080
4081pub fn proto_to_stash(entry: &proto::StashEntry) -> Result<StashEntry> {
4082    Ok(StashEntry {
4083        oid: Oid::from_bytes(&entry.oid)?,
4084        message: entry.message.clone(),
4085        index: entry.index as usize,
4086        branch: entry.branch.clone(),
4087        timestamp: entry.timestamp,
4088    })
4089}
4090
4091impl MergeDetails {
4092    async fn update(
4093        &mut self,
4094        backend: &Arc<dyn GitRepository>,
4095        current_conflicted_paths: Vec<RepoPath>,
4096    ) -> Result<bool> {
4097        log::debug!("load merge details");
4098        self.message = backend.merge_message().await.map(SharedString::from);
4099        let heads = backend
4100            .revparse_batch(vec![
4101                "MERGE_HEAD".into(),
4102                "CHERRY_PICK_HEAD".into(),
4103                "REBASE_HEAD".into(),
4104                "REVERT_HEAD".into(),
4105                "APPLY_HEAD".into(),
4106            ])
4107            .await
4108            .log_err()
4109            .unwrap_or_default()
4110            .into_iter()
4111            .map(|opt| opt.map(SharedString::from))
4112            .collect::<Vec<_>>();
4113
4114        let mut conflicts_changed = false;
4115
4116        // Record the merge state for newly conflicted paths
4117        for path in &current_conflicted_paths {
4118            if self.merge_heads_by_conflicted_path.get(&path).is_none() {
4119                conflicts_changed = true;
4120                self.merge_heads_by_conflicted_path
4121                    .insert(path.clone(), heads.clone());
4122            }
4123        }
4124
4125        // Clear state for paths that are no longer conflicted and for which the merge heads have changed
4126        self.merge_heads_by_conflicted_path
4127            .retain(|path, old_merge_heads| {
4128                let keep = current_conflicted_paths.contains(path)
4129                    || (old_merge_heads == &heads
4130                        && old_merge_heads.iter().any(|head| head.is_some()));
4131                if !keep {
4132                    conflicts_changed = true;
4133                }
4134                keep
4135            });
4136
4137        Ok(conflicts_changed)
4138    }
4139}
4140
4141impl Repository {
4142    pub fn is_trusted(&self) -> bool {
4143        match self.repository_state.peek() {
4144            Some(Ok(RepositoryState::Local(state))) => state.backend.is_trusted(),
4145            _ => false,
4146        }
4147    }
4148
4149    pub fn snapshot(&self) -> RepositorySnapshot {
4150        self.snapshot.clone()
4151    }
4152
4153    pub fn pending_ops(&self) -> impl Iterator<Item = PendingOps> + '_ {
4154        self.pending_ops.iter().cloned()
4155    }
4156
4157    pub fn pending_ops_summary(&self) -> PathSummary<PendingOpsSummary> {
4158        self.pending_ops.summary().clone()
4159    }
4160
4161    pub fn pending_ops_for_path(&self, path: &RepoPath) -> Option<PendingOps> {
4162        self.pending_ops
4163            .get(&PathKey(path.as_ref().clone()), ())
4164            .cloned()
4165    }
4166
4167    fn local(
4168        id: RepositoryId,
4169        work_directory_abs_path: Arc<Path>,
4170        original_repo_abs_path: Arc<Path>,
4171        dot_git_abs_path: Arc<Path>,
4172        project_environment: WeakEntity<ProjectEnvironment>,
4173        fs: Arc<dyn Fs>,
4174        is_trusted: bool,
4175        git_store: WeakEntity<GitStore>,
4176        cx: &mut Context<Self>,
4177    ) -> Self {
4178        let snapshot = RepositorySnapshot::empty(
4179            id,
4180            work_directory_abs_path.clone(),
4181            Some(original_repo_abs_path),
4182            PathStyle::local(),
4183        );
4184        let state = cx
4185            .spawn(async move |_, cx| {
4186                LocalRepositoryState::new(
4187                    work_directory_abs_path,
4188                    dot_git_abs_path,
4189                    project_environment,
4190                    fs,
4191                    is_trusted,
4192                    cx,
4193                )
4194                .await
4195                .map_err(|err| err.to_string())
4196            })
4197            .shared();
4198        let job_sender = Repository::spawn_local_git_worker(state.clone(), cx);
4199        let state = cx
4200            .spawn(async move |_, _| {
4201                let state = state.await?;
4202                Ok(RepositoryState::Local(state))
4203            })
4204            .shared();
4205
4206        cx.subscribe_self(move |this, event: &RepositoryEvent, _| match event {
4207            RepositoryEvent::HeadChanged | RepositoryEvent::BranchListChanged => {
4208                if this.scan_id > 1 {
4209                    this.initial_graph_data.clear();
4210                }
4211            }
4212            RepositoryEvent::StashEntriesChanged => {
4213                if this.scan_id > 1 {
4214                    this.initial_graph_data
4215                        .retain(|(log_source, _), _| *log_source != LogSource::All);
4216                }
4217            }
4218            _ => {}
4219        })
4220        .detach();
4221
4222        Repository {
4223            this: cx.weak_entity(),
4224            git_store,
4225            snapshot,
4226            pending_ops: Default::default(),
4227            repository_state: state,
4228            commit_message_buffer: None,
4229            askpass_delegates: Default::default(),
4230            paths_needing_status_update: Default::default(),
4231            latest_askpass_id: 0,
4232            job_sender,
4233            job_id: 0,
4234            active_jobs: Default::default(),
4235            initial_graph_data: Default::default(),
4236            commit_data: Default::default(),
4237            graph_commit_data_handler: GraphCommitHandlerState::Closed,
4238        }
4239    }
4240
4241    fn remote(
4242        id: RepositoryId,
4243        work_directory_abs_path: Arc<Path>,
4244        original_repo_abs_path: Option<Arc<Path>>,
4245        path_style: PathStyle,
4246        project_id: ProjectId,
4247        client: AnyProtoClient,
4248        git_store: WeakEntity<GitStore>,
4249        cx: &mut Context<Self>,
4250    ) -> Self {
4251        let snapshot = RepositorySnapshot::empty(
4252            id,
4253            work_directory_abs_path,
4254            original_repo_abs_path,
4255            path_style,
4256        );
4257        let repository_state = RemoteRepositoryState { project_id, client };
4258        let job_sender = Self::spawn_remote_git_worker(repository_state.clone(), cx);
4259        let repository_state = Task::ready(Ok(RepositoryState::Remote(repository_state))).shared();
4260        Self {
4261            this: cx.weak_entity(),
4262            snapshot,
4263            commit_message_buffer: None,
4264            git_store,
4265            pending_ops: Default::default(),
4266            paths_needing_status_update: Default::default(),
4267            job_sender,
4268            repository_state,
4269            askpass_delegates: Default::default(),
4270            latest_askpass_id: 0,
4271            active_jobs: Default::default(),
4272            job_id: 0,
4273            initial_graph_data: Default::default(),
4274            commit_data: Default::default(),
4275            graph_commit_data_handler: GraphCommitHandlerState::Closed,
4276        }
4277    }
4278
4279    pub fn git_store(&self) -> Option<Entity<GitStore>> {
4280        self.git_store.upgrade()
4281    }
4282
4283    fn reload_buffer_diff_bases(&mut self, cx: &mut Context<Self>) {
4284        let this = cx.weak_entity();
4285        let git_store = self.git_store.clone();
4286        let _ = self.send_keyed_job(
4287            Some(GitJobKey::ReloadBufferDiffBases),
4288            None,
4289            |state, mut cx| async move {
4290                let RepositoryState::Local(LocalRepositoryState { backend, .. }) = state else {
4291                    log::error!("tried to recompute diffs for a non-local repository");
4292                    return Ok(());
4293                };
4294
4295                let Some(this) = this.upgrade() else {
4296                    return Ok(());
4297                };
4298
4299                let repo_diff_state_updates = this.update(&mut cx, |this, cx| {
4300                    git_store.update(cx, |git_store, cx| {
4301                        git_store
4302                            .diffs
4303                            .iter()
4304                            .filter_map(|(buffer_id, diff_state)| {
4305                                let buffer_store = git_store.buffer_store.read(cx);
4306                                let buffer = buffer_store.get(*buffer_id)?;
4307                                let file = File::from_dyn(buffer.read(cx).file())?;
4308                                let abs_path = file.worktree.read(cx).absolutize(&file.path);
4309                                let repo_path = this.abs_path_to_repo_path(&abs_path)?;
4310                                log::debug!(
4311                                    "start reload diff bases for repo path {}",
4312                                    repo_path.as_unix_str()
4313                                );
4314                                diff_state.update(cx, |diff_state, _| {
4315                                    let has_unstaged_diff = diff_state
4316                                        .unstaged_diff
4317                                        .as_ref()
4318                                        .is_some_and(|diff| diff.is_upgradable());
4319                                    let has_uncommitted_diff = diff_state
4320                                        .uncommitted_diff
4321                                        .as_ref()
4322                                        .is_some_and(|set| set.is_upgradable());
4323
4324                                    Some((
4325                                        buffer,
4326                                        repo_path,
4327                                        has_unstaged_diff.then(|| diff_state.index_text.clone()),
4328                                        has_uncommitted_diff.then(|| diff_state.head_text.clone()),
4329                                    ))
4330                                })
4331                            })
4332                            .collect::<Vec<_>>()
4333                    })
4334                })?;
4335
4336                let buffer_diff_base_changes = cx
4337                    .background_spawn(async move {
4338                        let mut changes = Vec::new();
4339                        for (buffer, repo_path, current_index_text, current_head_text) in
4340                            &repo_diff_state_updates
4341                        {
4342                            let index_text = if current_index_text.is_some() {
4343                                backend.load_index_text(repo_path.clone()).await
4344                            } else {
4345                                None
4346                            };
4347                            let head_text = if current_head_text.is_some() {
4348                                backend.load_committed_text(repo_path.clone()).await
4349                            } else {
4350                                None
4351                            };
4352
4353                            let change =
4354                                match (current_index_text.as_ref(), current_head_text.as_ref()) {
4355                                    (Some(current_index), Some(current_head)) => {
4356                                        let index_changed =
4357                                            index_text.as_deref() != current_index.as_deref();
4358                                        let head_changed =
4359                                            head_text.as_deref() != current_head.as_deref();
4360                                        if index_changed && head_changed {
4361                                            if index_text == head_text {
4362                                                Some(DiffBasesChange::SetBoth(head_text))
4363                                            } else {
4364                                                Some(DiffBasesChange::SetEach {
4365                                                    index: index_text,
4366                                                    head: head_text,
4367                                                })
4368                                            }
4369                                        } else if index_changed {
4370                                            Some(DiffBasesChange::SetIndex(index_text))
4371                                        } else if head_changed {
4372                                            Some(DiffBasesChange::SetHead(head_text))
4373                                        } else {
4374                                            None
4375                                        }
4376                                    }
4377                                    (Some(current_index), None) => {
4378                                        let index_changed =
4379                                            index_text.as_deref() != current_index.as_deref();
4380                                        index_changed
4381                                            .then_some(DiffBasesChange::SetIndex(index_text))
4382                                    }
4383                                    (None, Some(current_head)) => {
4384                                        let head_changed =
4385                                            head_text.as_deref() != current_head.as_deref();
4386                                        head_changed.then_some(DiffBasesChange::SetHead(head_text))
4387                                    }
4388                                    (None, None) => None,
4389                                };
4390
4391                            changes.push((buffer.clone(), change))
4392                        }
4393                        changes
4394                    })
4395                    .await;
4396
4397                git_store.update(&mut cx, |git_store, cx| {
4398                    for (buffer, diff_bases_change) in buffer_diff_base_changes {
4399                        let buffer_snapshot = buffer.read(cx).text_snapshot();
4400                        let buffer_id = buffer_snapshot.remote_id();
4401                        let Some(diff_state) = git_store.diffs.get(&buffer_id) else {
4402                            continue;
4403                        };
4404
4405                        let downstream_client = git_store.downstream_client();
4406                        diff_state.update(cx, |diff_state, cx| {
4407                            use proto::update_diff_bases::Mode;
4408
4409                            if let Some((diff_bases_change, (client, project_id))) =
4410                                diff_bases_change.clone().zip(downstream_client)
4411                            {
4412                                let (staged_text, committed_text, mode) = match diff_bases_change {
4413                                    DiffBasesChange::SetIndex(index) => {
4414                                        (index, None, Mode::IndexOnly)
4415                                    }
4416                                    DiffBasesChange::SetHead(head) => (None, head, Mode::HeadOnly),
4417                                    DiffBasesChange::SetEach { index, head } => {
4418                                        (index, head, Mode::IndexAndHead)
4419                                    }
4420                                    DiffBasesChange::SetBoth(text) => {
4421                                        (None, text, Mode::IndexMatchesHead)
4422                                    }
4423                                };
4424                                client
4425                                    .send(proto::UpdateDiffBases {
4426                                        project_id: project_id.to_proto(),
4427                                        buffer_id: buffer_id.to_proto(),
4428                                        staged_text,
4429                                        committed_text,
4430                                        mode: mode as i32,
4431                                    })
4432                                    .log_err();
4433                            }
4434
4435                            diff_state.diff_bases_changed(buffer_snapshot, diff_bases_change, cx);
4436                        });
4437                    }
4438                })
4439            },
4440        );
4441    }
4442
4443    pub fn send_job<F, Fut, R>(
4444        &mut self,
4445        status: Option<SharedString>,
4446        job: F,
4447    ) -> oneshot::Receiver<R>
4448    where
4449        F: FnOnce(RepositoryState, AsyncApp) -> Fut + 'static,
4450        Fut: Future<Output = R> + 'static,
4451        R: Send + 'static,
4452    {
4453        self.send_keyed_job(None, status, job)
4454    }
4455
4456    fn send_keyed_job<F, Fut, R>(
4457        &mut self,
4458        key: Option<GitJobKey>,
4459        status: Option<SharedString>,
4460        job: F,
4461    ) -> oneshot::Receiver<R>
4462    where
4463        F: FnOnce(RepositoryState, AsyncApp) -> Fut + 'static,
4464        Fut: Future<Output = R> + 'static,
4465        R: Send + 'static,
4466    {
4467        let (result_tx, result_rx) = futures::channel::oneshot::channel();
4468        let job_id = post_inc(&mut self.job_id);
4469        let this = self.this.clone();
4470        self.job_sender
4471            .unbounded_send(GitJob {
4472                key,
4473                job: Box::new(move |state, cx: &mut AsyncApp| {
4474                    let job = job(state, cx.clone());
4475                    cx.spawn(async move |cx| {
4476                        if let Some(s) = status.clone() {
4477                            this.update(cx, |this, cx| {
4478                                this.active_jobs.insert(
4479                                    job_id,
4480                                    JobInfo {
4481                                        start: Instant::now(),
4482                                        message: s.clone(),
4483                                    },
4484                                );
4485
4486                                cx.notify();
4487                            })
4488                            .ok();
4489                        }
4490                        let result = job.await;
4491
4492                        this.update(cx, |this, cx| {
4493                            this.active_jobs.remove(&job_id);
4494                            cx.notify();
4495                        })
4496                        .ok();
4497
4498                        result_tx.send(result).ok();
4499                    })
4500                }),
4501            })
4502            .ok();
4503        result_rx
4504    }
4505
4506    pub fn set_as_active_repository(&self, cx: &mut Context<Self>) {
4507        let Some(git_store) = self.git_store.upgrade() else {
4508            return;
4509        };
4510        let entity = cx.entity();
4511        git_store.update(cx, |git_store, cx| {
4512            let Some((&id, _)) = git_store
4513                .repositories
4514                .iter()
4515                .find(|(_, handle)| *handle == &entity)
4516            else {
4517                return;
4518            };
4519            git_store.active_repo_id = Some(id);
4520            cx.emit(GitStoreEvent::ActiveRepositoryChanged(Some(id)));
4521        });
4522    }
4523
4524    pub fn cached_status(&self) -> impl '_ + Iterator<Item = StatusEntry> {
4525        self.snapshot.status()
4526    }
4527
4528    pub fn diff_stat_for_path(&self, path: &RepoPath) -> Option<DiffStat> {
4529        self.snapshot.diff_stat_for_path(path)
4530    }
4531
4532    pub fn cached_stash(&self) -> GitStash {
4533        self.snapshot.stash_entries.clone()
4534    }
4535
4536    pub fn repo_path_to_project_path(&self, path: &RepoPath, cx: &App) -> Option<ProjectPath> {
4537        let git_store = self.git_store.upgrade()?;
4538        let worktree_store = git_store.read(cx).worktree_store.read(cx);
4539        let abs_path = self.snapshot.repo_path_to_abs_path(path);
4540        let abs_path = SanitizedPath::new(&abs_path);
4541        let (worktree, relative_path) = worktree_store.find_worktree(abs_path, cx)?;
4542        Some(ProjectPath {
4543            worktree_id: worktree.read(cx).id(),
4544            path: relative_path,
4545        })
4546    }
4547
4548    pub fn project_path_to_repo_path(&self, path: &ProjectPath, cx: &App) -> Option<RepoPath> {
4549        let git_store = self.git_store.upgrade()?;
4550        let worktree_store = git_store.read(cx).worktree_store.read(cx);
4551        let abs_path = worktree_store.absolutize(path, cx)?;
4552        self.snapshot.abs_path_to_repo_path(&abs_path)
4553    }
4554
4555    pub fn contains_sub_repo(&self, other: &Entity<Self>, cx: &App) -> bool {
4556        other
4557            .read(cx)
4558            .snapshot
4559            .work_directory_abs_path
4560            .starts_with(&self.snapshot.work_directory_abs_path)
4561    }
4562
4563    pub fn open_commit_buffer(
4564        &mut self,
4565        languages: Option<Arc<LanguageRegistry>>,
4566        buffer_store: Entity<BufferStore>,
4567        cx: &mut Context<Self>,
4568    ) -> Task<Result<Entity<Buffer>>> {
4569        let id = self.id;
4570        if let Some(buffer) = self.commit_message_buffer.clone() {
4571            return Task::ready(Ok(buffer));
4572        }
4573        let this = cx.weak_entity();
4574
4575        let rx = self.send_job(None, move |state, mut cx| async move {
4576            let Some(this) = this.upgrade() else {
4577                bail!("git store was dropped");
4578            };
4579            match state {
4580                RepositoryState::Local(..) => {
4581                    this.update(&mut cx, |_, cx| {
4582                        Self::open_local_commit_buffer(languages, buffer_store, cx)
4583                    })
4584                    .await
4585                }
4586                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
4587                    let request = client.request(proto::OpenCommitMessageBuffer {
4588                        project_id: project_id.0,
4589                        repository_id: id.to_proto(),
4590                    });
4591                    let response = request.await.context("requesting to open commit buffer")?;
4592                    let buffer_id = BufferId::new(response.buffer_id)?;
4593                    let buffer = buffer_store
4594                        .update(&mut cx, |buffer_store, cx| {
4595                            buffer_store.wait_for_remote_buffer(buffer_id, cx)
4596                        })
4597                        .await?;
4598                    if let Some(language_registry) = languages {
4599                        let git_commit_language =
4600                            language_registry.language_for_name("Git Commit").await?;
4601                        buffer.update(&mut cx, |buffer, cx| {
4602                            buffer.set_language(Some(git_commit_language), cx);
4603                        });
4604                    }
4605                    this.update(&mut cx, |this, _| {
4606                        this.commit_message_buffer = Some(buffer.clone());
4607                    });
4608                    Ok(buffer)
4609                }
4610            }
4611        });
4612
4613        cx.spawn(|_, _: &mut AsyncApp| async move { rx.await? })
4614    }
4615
4616    fn open_local_commit_buffer(
4617        language_registry: Option<Arc<LanguageRegistry>>,
4618        buffer_store: Entity<BufferStore>,
4619        cx: &mut Context<Self>,
4620    ) -> Task<Result<Entity<Buffer>>> {
4621        cx.spawn(async move |repository, cx| {
4622            let git_commit_language = match language_registry {
4623                Some(language_registry) => {
4624                    Some(language_registry.language_for_name("Git Commit").await?)
4625                }
4626                None => None,
4627            };
4628            let buffer = buffer_store
4629                .update(cx, |buffer_store, cx| {
4630                    buffer_store.create_buffer(git_commit_language, false, cx)
4631                })
4632                .await?;
4633
4634            repository.update(cx, |repository, _| {
4635                repository.commit_message_buffer = Some(buffer.clone());
4636            })?;
4637            Ok(buffer)
4638        })
4639    }
4640
4641    pub fn checkout_files(
4642        &mut self,
4643        commit: &str,
4644        paths: Vec<RepoPath>,
4645        cx: &mut Context<Self>,
4646    ) -> Task<Result<()>> {
4647        let commit = commit.to_string();
4648        let id = self.id;
4649
4650        self.spawn_job_with_tracking(
4651            paths.clone(),
4652            pending_op::GitStatus::Reverted,
4653            cx,
4654            async move |this, cx| {
4655                this.update(cx, |this, _cx| {
4656                    this.send_job(
4657                        Some(format!("git checkout {}", commit).into()),
4658                        move |git_repo, _| async move {
4659                            match git_repo {
4660                                RepositoryState::Local(LocalRepositoryState {
4661                                    backend,
4662                                    environment,
4663                                    ..
4664                                }) => {
4665                                    backend
4666                                        .checkout_files(commit, paths, environment.clone())
4667                                        .await
4668                                }
4669                                RepositoryState::Remote(RemoteRepositoryState {
4670                                    project_id,
4671                                    client,
4672                                }) => {
4673                                    client
4674                                        .request(proto::GitCheckoutFiles {
4675                                            project_id: project_id.0,
4676                                            repository_id: id.to_proto(),
4677                                            commit,
4678                                            paths: paths
4679                                                .into_iter()
4680                                                .map(|p| p.to_proto())
4681                                                .collect(),
4682                                        })
4683                                        .await?;
4684
4685                                    Ok(())
4686                                }
4687                            }
4688                        },
4689                    )
4690                })?
4691                .await?
4692            },
4693        )
4694    }
4695
4696    pub fn reset(
4697        &mut self,
4698        commit: String,
4699        reset_mode: ResetMode,
4700        _cx: &mut App,
4701    ) -> oneshot::Receiver<Result<()>> {
4702        let id = self.id;
4703
4704        self.send_job(None, move |git_repo, _| async move {
4705            match git_repo {
4706                RepositoryState::Local(LocalRepositoryState {
4707                    backend,
4708                    environment,
4709                    ..
4710                }) => backend.reset(commit, reset_mode, environment).await,
4711                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
4712                    client
4713                        .request(proto::GitReset {
4714                            project_id: project_id.0,
4715                            repository_id: id.to_proto(),
4716                            commit,
4717                            mode: match reset_mode {
4718                                ResetMode::Soft => git_reset::ResetMode::Soft.into(),
4719                                ResetMode::Mixed => git_reset::ResetMode::Mixed.into(),
4720                            },
4721                        })
4722                        .await?;
4723
4724                    Ok(())
4725                }
4726            }
4727        })
4728    }
4729
4730    pub fn show(&mut self, commit: String) -> oneshot::Receiver<Result<CommitDetails>> {
4731        let id = self.id;
4732        self.send_job(None, move |git_repo, _cx| async move {
4733            match git_repo {
4734                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
4735                    backend.show(commit).await
4736                }
4737                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
4738                    let resp = client
4739                        .request(proto::GitShow {
4740                            project_id: project_id.0,
4741                            repository_id: id.to_proto(),
4742                            commit,
4743                        })
4744                        .await?;
4745
4746                    Ok(CommitDetails {
4747                        sha: resp.sha.into(),
4748                        message: resp.message.into(),
4749                        commit_timestamp: resp.commit_timestamp,
4750                        author_email: resp.author_email.into(),
4751                        author_name: resp.author_name.into(),
4752                    })
4753                }
4754            }
4755        })
4756    }
4757
4758    pub fn load_commit_diff(&mut self, commit: String) -> oneshot::Receiver<Result<CommitDiff>> {
4759        let id = self.id;
4760        self.send_job(None, move |git_repo, cx| async move {
4761            match git_repo {
4762                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
4763                    backend.load_commit(commit, cx).await
4764                }
4765                RepositoryState::Remote(RemoteRepositoryState {
4766                    client, project_id, ..
4767                }) => {
4768                    let response = client
4769                        .request(proto::LoadCommitDiff {
4770                            project_id: project_id.0,
4771                            repository_id: id.to_proto(),
4772                            commit,
4773                        })
4774                        .await?;
4775                    Ok(CommitDiff {
4776                        files: response
4777                            .files
4778                            .into_iter()
4779                            .map(|file| {
4780                                Ok(CommitFile {
4781                                    path: RepoPath::from_proto(&file.path)?,
4782                                    old_text: file.old_text,
4783                                    new_text: file.new_text,
4784                                    is_binary: file.is_binary,
4785                                })
4786                            })
4787                            .collect::<Result<Vec<_>>>()?,
4788                    })
4789                }
4790            }
4791        })
4792    }
4793
4794    pub fn file_history(
4795        &mut self,
4796        path: RepoPath,
4797    ) -> oneshot::Receiver<Result<git::repository::FileHistory>> {
4798        self.file_history_paginated(path, 0, None)
4799    }
4800
4801    pub fn file_history_paginated(
4802        &mut self,
4803        path: RepoPath,
4804        skip: usize,
4805        limit: Option<usize>,
4806    ) -> oneshot::Receiver<Result<git::repository::FileHistory>> {
4807        let id = self.id;
4808        self.send_job(None, move |git_repo, _cx| async move {
4809            match git_repo {
4810                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
4811                    backend.file_history_paginated(path, skip, limit).await
4812                }
4813                RepositoryState::Remote(RemoteRepositoryState { client, project_id }) => {
4814                    let response = client
4815                        .request(proto::GitFileHistory {
4816                            project_id: project_id.0,
4817                            repository_id: id.to_proto(),
4818                            path: path.to_proto(),
4819                            skip: skip as u64,
4820                            limit: limit.map(|l| l as u64),
4821                        })
4822                        .await?;
4823                    Ok(git::repository::FileHistory {
4824                        entries: response
4825                            .entries
4826                            .into_iter()
4827                            .map(|entry| git::repository::FileHistoryEntry {
4828                                sha: entry.sha.into(),
4829                                subject: entry.subject.into(),
4830                                message: entry.message.into(),
4831                                commit_timestamp: entry.commit_timestamp,
4832                                author_name: entry.author_name.into(),
4833                                author_email: entry.author_email.into(),
4834                            })
4835                            .collect(),
4836                        path: RepoPath::from_proto(&response.path)?,
4837                    })
4838                }
4839            }
4840        })
4841    }
4842
4843    pub fn get_graph_data(
4844        &self,
4845        log_source: LogSource,
4846        log_order: LogOrder,
4847    ) -> Option<&InitialGitGraphData> {
4848        self.initial_graph_data.get(&(log_source, log_order))
4849    }
4850
4851    pub fn search_commits(
4852        &mut self,
4853        log_source: LogSource,
4854        search_args: SearchCommitArgs,
4855        request_tx: smol::channel::Sender<Oid>,
4856        cx: &mut Context<Self>,
4857    ) {
4858        let repository_state = self.repository_state.clone();
4859
4860        cx.background_spawn(async move {
4861            let repo_state = repository_state.await;
4862
4863            match repo_state {
4864                Ok(RepositoryState::Local(LocalRepositoryState { backend, .. })) => {
4865                    backend
4866                        .search_commits(log_source, search_args, request_tx)
4867                        .await
4868                        .log_err();
4869                }
4870                Ok(RepositoryState::Remote(_)) => {}
4871                Err(_) => {}
4872            };
4873        })
4874        .detach();
4875    }
4876
4877    pub fn graph_data(
4878        &mut self,
4879        log_source: LogSource,
4880        log_order: LogOrder,
4881        range: Range<usize>,
4882        cx: &mut Context<Self>,
4883    ) -> GraphDataResponse<'_> {
4884        let initial_commit_data = self
4885            .initial_graph_data
4886            .entry((log_source.clone(), log_order))
4887            .or_insert_with(|| {
4888                let state = self.repository_state.clone();
4889                let log_source = log_source.clone();
4890
4891                let fetch_task = cx.spawn(async move |repository, cx| {
4892                    let state = state.await;
4893                    let result = match state {
4894                        Ok(RepositoryState::Local(LocalRepositoryState { backend, .. })) => {
4895                            Self::local_git_graph_data(
4896                                repository.clone(),
4897                                backend,
4898                                log_source.clone(),
4899                                log_order,
4900                                cx,
4901                            )
4902                            .await
4903                        }
4904                        Ok(RepositoryState::Remote(_)) => {
4905                            Err("Git graph is not supported for collab yet".into())
4906                        }
4907                        Err(e) => Err(SharedString::from(e)),
4908                    };
4909
4910                    if let Err(fetch_task_error) = result {
4911                        repository
4912                            .update(cx, |repository, _| {
4913                                if let Some(data) = repository
4914                                    .initial_graph_data
4915                                    .get_mut(&(log_source, log_order))
4916                                {
4917                                    data.error = Some(fetch_task_error);
4918                                } else {
4919                                    debug_panic!(
4920                                        "This task would be dropped if this entry doesn't exist"
4921                                    );
4922                                }
4923                            })
4924                            .ok();
4925                    }
4926                });
4927
4928                InitialGitGraphData {
4929                    fetch_task,
4930                    error: None,
4931                    commit_data: Vec::new(),
4932                    commit_oid_to_index: HashMap::default(),
4933                }
4934            });
4935
4936        let max_start = initial_commit_data.commit_data.len().saturating_sub(1);
4937        let max_end = initial_commit_data.commit_data.len();
4938
4939        GraphDataResponse {
4940            commits: &initial_commit_data.commit_data
4941                [range.start.min(max_start)..range.end.min(max_end)],
4942            is_loading: !initial_commit_data.fetch_task.is_ready(),
4943            error: initial_commit_data.error.clone(),
4944        }
4945    }
4946
4947    async fn local_git_graph_data(
4948        this: WeakEntity<Self>,
4949        backend: Arc<dyn GitRepository>,
4950        log_source: LogSource,
4951        log_order: LogOrder,
4952        cx: &mut AsyncApp,
4953    ) -> Result<(), SharedString> {
4954        let (request_tx, request_rx) =
4955            smol::channel::unbounded::<Vec<Arc<InitialGraphCommitData>>>();
4956
4957        let task = cx.background_executor().spawn({
4958            let log_source = log_source.clone();
4959            async move {
4960                backend
4961                    .initial_graph_data(log_source, log_order, request_tx)
4962                    .await
4963                    .map_err(|err| SharedString::from(err.to_string()))
4964            }
4965        });
4966
4967        let graph_data_key = (log_source, log_order);
4968
4969        while let Ok(initial_graph_commit_data) = request_rx.recv().await {
4970            this.update(cx, |repository, cx| {
4971                let graph_data = repository
4972                    .initial_graph_data
4973                    .entry(graph_data_key.clone())
4974                    .and_modify(|graph_data| {
4975                        for commit_data in initial_graph_commit_data {
4976                            graph_data
4977                                .commit_oid_to_index
4978                                .insert(commit_data.sha, graph_data.commit_data.len());
4979                            graph_data.commit_data.push(commit_data);
4980                        }
4981                        cx.emit(RepositoryEvent::GraphEvent(
4982                            graph_data_key.clone(),
4983                            GitGraphEvent::CountUpdated(graph_data.commit_data.len()),
4984                        ));
4985                    });
4986
4987                match &graph_data {
4988                    Entry::Occupied(_) => {}
4989                    Entry::Vacant(_) => {
4990                        debug_panic!("This task should be dropped if data doesn't exist");
4991                    }
4992                }
4993            })
4994            .ok();
4995        }
4996
4997        task.await?;
4998        Ok(())
4999    }
5000
5001    pub fn fetch_commit_data(&mut self, sha: Oid, cx: &mut Context<Self>) -> &CommitDataState {
5002        if !self.commit_data.contains_key(&sha) {
5003            match &self.graph_commit_data_handler {
5004                GraphCommitHandlerState::Open(handler) => {
5005                    if handler.commit_data_request.try_send(sha).is_ok() {
5006                        let old_value = self.commit_data.insert(sha, CommitDataState::Loading);
5007                        debug_assert!(old_value.is_none(), "We should never overwrite commit data");
5008                    }
5009                }
5010                GraphCommitHandlerState::Closed => {
5011                    self.open_graph_commit_data_handler(cx);
5012                }
5013                GraphCommitHandlerState::Starting => {}
5014            }
5015        }
5016
5017        self.commit_data
5018            .get(&sha)
5019            .unwrap_or(&CommitDataState::Loading)
5020    }
5021
5022    fn open_graph_commit_data_handler(&mut self, cx: &mut Context<Self>) {
5023        self.graph_commit_data_handler = GraphCommitHandlerState::Starting;
5024
5025        let state = self.repository_state.clone();
5026        let (result_tx, result_rx) = smol::channel::bounded::<(Oid, GraphCommitData)>(64);
5027        let (request_tx, request_rx) = smol::channel::unbounded::<Oid>();
5028
5029        let foreground_task = cx.spawn(async move |this, cx| {
5030            while let Ok((sha, commit_data)) = result_rx.recv().await {
5031                let result = this.update(cx, |this, cx| {
5032                    let old_value = this
5033                        .commit_data
5034                        .insert(sha, CommitDataState::Loaded(Arc::new(commit_data)));
5035                    debug_assert!(
5036                        !matches!(old_value, Some(CommitDataState::Loaded(_))),
5037                        "We should never overwrite commit data"
5038                    );
5039
5040                    cx.notify();
5041                });
5042                if result.is_err() {
5043                    break;
5044                }
5045            }
5046
5047            this.update(cx, |this, _cx| {
5048                this.graph_commit_data_handler = GraphCommitHandlerState::Closed;
5049            })
5050            .ok();
5051        });
5052
5053        let request_tx_for_handler = request_tx;
5054        let background_executor = cx.background_executor().clone();
5055
5056        cx.background_spawn(async move {
5057            let backend = match state.await {
5058                Ok(RepositoryState::Local(LocalRepositoryState { backend, .. })) => backend,
5059                Ok(RepositoryState::Remote(_)) => {
5060                    log::error!("commit_data_reader not supported for remote repositories");
5061                    return;
5062                }
5063                Err(error) => {
5064                    log::error!("failed to get repository state: {error}");
5065                    return;
5066                }
5067            };
5068
5069            let reader = match backend.commit_data_reader() {
5070                Ok(reader) => reader,
5071                Err(error) => {
5072                    log::error!("failed to create commit data reader: {error:?}");
5073                    return;
5074                }
5075            };
5076
5077            loop {
5078                let timeout = background_executor.timer(std::time::Duration::from_secs(10));
5079
5080                futures::select_biased! {
5081                    sha = futures::FutureExt::fuse(request_rx.recv()) => {
5082                        let Ok(sha) = sha else {
5083                            break;
5084                        };
5085
5086                        match reader.read(sha).await {
5087                            Ok(commit_data) => {
5088                                if result_tx.send((sha, commit_data)).await.is_err() {
5089                                    break;
5090                                }
5091                            }
5092                            Err(error) => {
5093                                log::error!("failed to read commit data for {sha}: {error:?}");
5094                            }
5095                        }
5096                    }
5097                    _ = futures::FutureExt::fuse(timeout) => {
5098                        break;
5099                    }
5100                }
5101            }
5102
5103            drop(result_tx);
5104        })
5105        .detach();
5106
5107        self.graph_commit_data_handler = GraphCommitHandlerState::Open(GraphCommitDataHandler {
5108            _task: foreground_task,
5109            commit_data_request: request_tx_for_handler,
5110        });
5111    }
5112
5113    fn buffer_store(&self, cx: &App) -> Option<Entity<BufferStore>> {
5114        Some(self.git_store.upgrade()?.read(cx).buffer_store.clone())
5115    }
5116
5117    fn save_buffers<'a>(
5118        &self,
5119        entries: impl IntoIterator<Item = &'a RepoPath>,
5120        cx: &mut Context<Self>,
5121    ) -> Vec<Task<anyhow::Result<()>>> {
5122        let mut save_futures = Vec::new();
5123        if let Some(buffer_store) = self.buffer_store(cx) {
5124            buffer_store.update(cx, |buffer_store, cx| {
5125                for path in entries {
5126                    let Some(project_path) = self.repo_path_to_project_path(path, cx) else {
5127                        continue;
5128                    };
5129                    if let Some(buffer) = buffer_store.get_by_path(&project_path)
5130                        && buffer
5131                            .read(cx)
5132                            .file()
5133                            .is_some_and(|file| file.disk_state().exists())
5134                        && buffer.read(cx).has_unsaved_edits()
5135                    {
5136                        save_futures.push(buffer_store.save_buffer(buffer, cx));
5137                    }
5138                }
5139            })
5140        }
5141        save_futures
5142    }
5143
5144    pub fn stage_entries(
5145        &mut self,
5146        entries: Vec<RepoPath>,
5147        cx: &mut Context<Self>,
5148    ) -> Task<anyhow::Result<()>> {
5149        self.stage_or_unstage_entries(true, entries, cx)
5150    }
5151
5152    pub fn unstage_entries(
5153        &mut self,
5154        entries: Vec<RepoPath>,
5155        cx: &mut Context<Self>,
5156    ) -> Task<anyhow::Result<()>> {
5157        self.stage_or_unstage_entries(false, entries, cx)
5158    }
5159
5160    fn stage_or_unstage_entries(
5161        &mut self,
5162        stage: bool,
5163        entries: Vec<RepoPath>,
5164        cx: &mut Context<Self>,
5165    ) -> Task<anyhow::Result<()>> {
5166        if entries.is_empty() {
5167            return Task::ready(Ok(()));
5168        }
5169        let Some(git_store) = self.git_store.upgrade() else {
5170            return Task::ready(Ok(()));
5171        };
5172        let id = self.id;
5173        let save_tasks = self.save_buffers(&entries, cx);
5174        let paths = entries
5175            .iter()
5176            .map(|p| p.as_unix_str())
5177            .collect::<Vec<_>>()
5178            .join(" ");
5179        let status = if stage {
5180            format!("git add {paths}")
5181        } else {
5182            format!("git reset {paths}")
5183        };
5184        let job_key = GitJobKey::WriteIndex(entries.clone());
5185
5186        self.spawn_job_with_tracking(
5187            entries.clone(),
5188            if stage {
5189                pending_op::GitStatus::Staged
5190            } else {
5191                pending_op::GitStatus::Unstaged
5192            },
5193            cx,
5194            async move |this, cx| {
5195                for save_task in save_tasks {
5196                    save_task.await?;
5197                }
5198
5199                this.update(cx, |this, cx| {
5200                    let weak_this = cx.weak_entity();
5201                    this.send_keyed_job(
5202                        Some(job_key),
5203                        Some(status.into()),
5204                        move |git_repo, mut cx| async move {
5205                            let hunk_staging_operation_counts = weak_this
5206                                .update(&mut cx, |this, cx| {
5207                                    let mut hunk_staging_operation_counts = HashMap::default();
5208                                    for path in &entries {
5209                                        let Some(project_path) =
5210                                            this.repo_path_to_project_path(path, cx)
5211                                        else {
5212                                            continue;
5213                                        };
5214                                        let Some(buffer) = git_store
5215                                            .read(cx)
5216                                            .buffer_store
5217                                            .read(cx)
5218                                            .get_by_path(&project_path)
5219                                        else {
5220                                            continue;
5221                                        };
5222                                        let Some(diff_state) = git_store
5223                                            .read(cx)
5224                                            .diffs
5225                                            .get(&buffer.read(cx).remote_id())
5226                                            .cloned()
5227                                        else {
5228                                            continue;
5229                                        };
5230                                        let Some(uncommitted_diff) =
5231                                            diff_state.read(cx).uncommitted_diff.as_ref().and_then(
5232                                                |uncommitted_diff| uncommitted_diff.upgrade(),
5233                                            )
5234                                        else {
5235                                            continue;
5236                                        };
5237                                        let buffer_snapshot = buffer.read(cx).text_snapshot();
5238                                        let file_exists = buffer
5239                                            .read(cx)
5240                                            .file()
5241                                            .is_some_and(|file| file.disk_state().exists());
5242                                        let hunk_staging_operation_count =
5243                                            diff_state.update(cx, |diff_state, cx| {
5244                                                uncommitted_diff.update(
5245                                                    cx,
5246                                                    |uncommitted_diff, cx| {
5247                                                        uncommitted_diff
5248                                                            .stage_or_unstage_all_hunks(
5249                                                                stage,
5250                                                                &buffer_snapshot,
5251                                                                file_exists,
5252                                                                cx,
5253                                                            );
5254                                                    },
5255                                                );
5256
5257                                                diff_state.hunk_staging_operation_count += 1;
5258                                                diff_state.hunk_staging_operation_count
5259                                            });
5260                                        hunk_staging_operation_counts.insert(
5261                                            diff_state.downgrade(),
5262                                            hunk_staging_operation_count,
5263                                        );
5264                                    }
5265                                    hunk_staging_operation_counts
5266                                })
5267                                .unwrap_or_default();
5268
5269                            let result = match git_repo {
5270                                RepositoryState::Local(LocalRepositoryState {
5271                                    backend,
5272                                    environment,
5273                                    ..
5274                                }) => {
5275                                    if stage {
5276                                        backend.stage_paths(entries, environment.clone()).await
5277                                    } else {
5278                                        backend.unstage_paths(entries, environment.clone()).await
5279                                    }
5280                                }
5281                                RepositoryState::Remote(RemoteRepositoryState {
5282                                    project_id,
5283                                    client,
5284                                }) => {
5285                                    if stage {
5286                                        client
5287                                            .request(proto::Stage {
5288                                                project_id: project_id.0,
5289                                                repository_id: id.to_proto(),
5290                                                paths: entries
5291                                                    .into_iter()
5292                                                    .map(|repo_path| repo_path.to_proto())
5293                                                    .collect(),
5294                                            })
5295                                            .await
5296                                            .context("sending stage request")
5297                                            .map(|_| ())
5298                                    } else {
5299                                        client
5300                                            .request(proto::Unstage {
5301                                                project_id: project_id.0,
5302                                                repository_id: id.to_proto(),
5303                                                paths: entries
5304                                                    .into_iter()
5305                                                    .map(|repo_path| repo_path.to_proto())
5306                                                    .collect(),
5307                                            })
5308                                            .await
5309                                            .context("sending unstage request")
5310                                            .map(|_| ())
5311                                    }
5312                                }
5313                            };
5314
5315                            for (diff_state, hunk_staging_operation_count) in
5316                                hunk_staging_operation_counts
5317                            {
5318                                diff_state
5319                                    .update(&mut cx, |diff_state, cx| {
5320                                        if result.is_ok() {
5321                                            diff_state.hunk_staging_operation_count_as_of_write =
5322                                                hunk_staging_operation_count;
5323                                        } else if let Some(uncommitted_diff) =
5324                                            &diff_state.uncommitted_diff
5325                                        {
5326                                            uncommitted_diff
5327                                                .update(cx, |uncommitted_diff, cx| {
5328                                                    uncommitted_diff.clear_pending_hunks(cx);
5329                                                })
5330                                                .ok();
5331                                        }
5332                                    })
5333                                    .ok();
5334                            }
5335
5336                            result
5337                        },
5338                    )
5339                })?
5340                .await?
5341            },
5342        )
5343    }
5344
5345    pub fn stage_all(&mut self, cx: &mut Context<Self>) -> Task<anyhow::Result<()>> {
5346        let snapshot = self.snapshot.clone();
5347        let pending_ops = self.pending_ops.clone();
5348        let to_stage = cx.background_spawn(async move {
5349            snapshot
5350                .status()
5351                .filter_map(|entry| {
5352                    if let Some(ops) =
5353                        pending_ops.get(&PathKey(entry.repo_path.as_ref().clone()), ())
5354                    {
5355                        if ops.staging() || ops.staged() {
5356                            None
5357                        } else {
5358                            Some(entry.repo_path)
5359                        }
5360                    } else if entry.status.staging().is_fully_staged() {
5361                        None
5362                    } else {
5363                        Some(entry.repo_path)
5364                    }
5365                })
5366                .collect()
5367        });
5368
5369        cx.spawn(async move |this, cx| {
5370            let to_stage = to_stage.await;
5371            this.update(cx, |this, cx| {
5372                this.stage_or_unstage_entries(true, to_stage, cx)
5373            })?
5374            .await
5375        })
5376    }
5377
5378    pub fn unstage_all(&mut self, cx: &mut Context<Self>) -> Task<anyhow::Result<()>> {
5379        let snapshot = self.snapshot.clone();
5380        let pending_ops = self.pending_ops.clone();
5381        let to_unstage = cx.background_spawn(async move {
5382            snapshot
5383                .status()
5384                .filter_map(|entry| {
5385                    if let Some(ops) =
5386                        pending_ops.get(&PathKey(entry.repo_path.as_ref().clone()), ())
5387                    {
5388                        if !ops.staging() && !ops.staged() {
5389                            None
5390                        } else {
5391                            Some(entry.repo_path)
5392                        }
5393                    } else if entry.status.staging().is_fully_unstaged() {
5394                        None
5395                    } else {
5396                        Some(entry.repo_path)
5397                    }
5398                })
5399                .collect()
5400        });
5401
5402        cx.spawn(async move |this, cx| {
5403            let to_unstage = to_unstage.await;
5404            this.update(cx, |this, cx| {
5405                this.stage_or_unstage_entries(false, to_unstage, cx)
5406            })?
5407            .await
5408        })
5409    }
5410
5411    pub fn stash_all(&mut self, cx: &mut Context<Self>) -> Task<anyhow::Result<()>> {
5412        let to_stash = self.cached_status().map(|entry| entry.repo_path).collect();
5413
5414        self.stash_entries(to_stash, cx)
5415    }
5416
5417    pub fn stash_entries(
5418        &mut self,
5419        entries: Vec<RepoPath>,
5420        cx: &mut Context<Self>,
5421    ) -> Task<anyhow::Result<()>> {
5422        let id = self.id;
5423
5424        cx.spawn(async move |this, cx| {
5425            this.update(cx, |this, _| {
5426                this.send_job(None, move |git_repo, _cx| async move {
5427                    match git_repo {
5428                        RepositoryState::Local(LocalRepositoryState {
5429                            backend,
5430                            environment,
5431                            ..
5432                        }) => backend.stash_paths(entries, environment).await,
5433                        RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5434                            client
5435                                .request(proto::Stash {
5436                                    project_id: project_id.0,
5437                                    repository_id: id.to_proto(),
5438                                    paths: entries
5439                                        .into_iter()
5440                                        .map(|repo_path| repo_path.to_proto())
5441                                        .collect(),
5442                                })
5443                                .await?;
5444                            Ok(())
5445                        }
5446                    }
5447                })
5448            })?
5449            .await??;
5450            Ok(())
5451        })
5452    }
5453
5454    pub fn stash_pop(
5455        &mut self,
5456        index: Option<usize>,
5457        cx: &mut Context<Self>,
5458    ) -> Task<anyhow::Result<()>> {
5459        let id = self.id;
5460        cx.spawn(async move |this, cx| {
5461            this.update(cx, |this, _| {
5462                this.send_job(None, move |git_repo, _cx| async move {
5463                    match git_repo {
5464                        RepositoryState::Local(LocalRepositoryState {
5465                            backend,
5466                            environment,
5467                            ..
5468                        }) => backend.stash_pop(index, environment).await,
5469                        RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5470                            client
5471                                .request(proto::StashPop {
5472                                    project_id: project_id.0,
5473                                    repository_id: id.to_proto(),
5474                                    stash_index: index.map(|i| i as u64),
5475                                })
5476                                .await
5477                                .context("sending stash pop request")?;
5478                            Ok(())
5479                        }
5480                    }
5481                })
5482            })?
5483            .await??;
5484            Ok(())
5485        })
5486    }
5487
5488    pub fn stash_apply(
5489        &mut self,
5490        index: Option<usize>,
5491        cx: &mut Context<Self>,
5492    ) -> Task<anyhow::Result<()>> {
5493        let id = self.id;
5494        cx.spawn(async move |this, cx| {
5495            this.update(cx, |this, _| {
5496                this.send_job(None, move |git_repo, _cx| async move {
5497                    match git_repo {
5498                        RepositoryState::Local(LocalRepositoryState {
5499                            backend,
5500                            environment,
5501                            ..
5502                        }) => backend.stash_apply(index, environment).await,
5503                        RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5504                            client
5505                                .request(proto::StashApply {
5506                                    project_id: project_id.0,
5507                                    repository_id: id.to_proto(),
5508                                    stash_index: index.map(|i| i as u64),
5509                                })
5510                                .await
5511                                .context("sending stash apply request")?;
5512                            Ok(())
5513                        }
5514                    }
5515                })
5516            })?
5517            .await??;
5518            Ok(())
5519        })
5520    }
5521
5522    pub fn stash_drop(
5523        &mut self,
5524        index: Option<usize>,
5525        cx: &mut Context<Self>,
5526    ) -> oneshot::Receiver<anyhow::Result<()>> {
5527        let id = self.id;
5528        let updates_tx = self
5529            .git_store()
5530            .and_then(|git_store| match &git_store.read(cx).state {
5531                GitStoreState::Local { downstream, .. } => downstream
5532                    .as_ref()
5533                    .map(|downstream| downstream.updates_tx.clone()),
5534                _ => None,
5535            });
5536        let this = cx.weak_entity();
5537        self.send_job(None, move |git_repo, mut cx| async move {
5538            match git_repo {
5539                RepositoryState::Local(LocalRepositoryState {
5540                    backend,
5541                    environment,
5542                    ..
5543                }) => {
5544                    // TODO would be nice to not have to do this manually
5545                    let result = backend.stash_drop(index, environment).await;
5546                    if result.is_ok()
5547                        && let Ok(stash_entries) = backend.stash_entries().await
5548                    {
5549                        let snapshot = this.update(&mut cx, |this, cx| {
5550                            this.snapshot.stash_entries = stash_entries;
5551                            cx.emit(RepositoryEvent::StashEntriesChanged);
5552                            this.snapshot.clone()
5553                        })?;
5554                        if let Some(updates_tx) = updates_tx {
5555                            updates_tx
5556                                .unbounded_send(DownstreamUpdate::UpdateRepository(snapshot))
5557                                .ok();
5558                        }
5559                    }
5560
5561                    result
5562                }
5563                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5564                    client
5565                        .request(proto::StashDrop {
5566                            project_id: project_id.0,
5567                            repository_id: id.to_proto(),
5568                            stash_index: index.map(|i| i as u64),
5569                        })
5570                        .await
5571                        .context("sending stash pop request")?;
5572                    Ok(())
5573                }
5574            }
5575        })
5576    }
5577
5578    pub fn run_hook(&mut self, hook: RunHook, _cx: &mut App) -> oneshot::Receiver<Result<()>> {
5579        let id = self.id;
5580        self.send_job(
5581            Some(format!("git hook {}", hook.as_str()).into()),
5582            move |git_repo, _cx| async move {
5583                match git_repo {
5584                    RepositoryState::Local(LocalRepositoryState {
5585                        backend,
5586                        environment,
5587                        ..
5588                    }) => backend.run_hook(hook, environment.clone()).await,
5589                    RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5590                        client
5591                            .request(proto::RunGitHook {
5592                                project_id: project_id.0,
5593                                repository_id: id.to_proto(),
5594                                hook: hook.to_proto(),
5595                            })
5596                            .await?;
5597
5598                        Ok(())
5599                    }
5600                }
5601            },
5602        )
5603    }
5604
5605    pub fn commit(
5606        &mut self,
5607        message: SharedString,
5608        name_and_email: Option<(SharedString, SharedString)>,
5609        options: CommitOptions,
5610        askpass: AskPassDelegate,
5611        cx: &mut App,
5612    ) -> oneshot::Receiver<Result<()>> {
5613        let id = self.id;
5614        let askpass_delegates = self.askpass_delegates.clone();
5615        let askpass_id = util::post_inc(&mut self.latest_askpass_id);
5616
5617        let rx = self.run_hook(RunHook::PreCommit, cx);
5618
5619        self.send_job(Some("git commit".into()), move |git_repo, _cx| async move {
5620            rx.await??;
5621
5622            match git_repo {
5623                RepositoryState::Local(LocalRepositoryState {
5624                    backend,
5625                    environment,
5626                    ..
5627                }) => {
5628                    backend
5629                        .commit(message, name_and_email, options, askpass, environment)
5630                        .await
5631                }
5632                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5633                    askpass_delegates.lock().insert(askpass_id, askpass);
5634                    let _defer = util::defer(|| {
5635                        let askpass_delegate = askpass_delegates.lock().remove(&askpass_id);
5636                        debug_assert!(askpass_delegate.is_some());
5637                    });
5638                    let (name, email) = name_and_email.unzip();
5639                    client
5640                        .request(proto::Commit {
5641                            project_id: project_id.0,
5642                            repository_id: id.to_proto(),
5643                            message: String::from(message),
5644                            name: name.map(String::from),
5645                            email: email.map(String::from),
5646                            options: Some(proto::commit::CommitOptions {
5647                                amend: options.amend,
5648                                signoff: options.signoff,
5649                                allow_empty: options.allow_empty,
5650                            }),
5651                            askpass_id,
5652                        })
5653                        .await?;
5654
5655                    Ok(())
5656                }
5657            }
5658        })
5659    }
5660
5661    pub fn fetch(
5662        &mut self,
5663        fetch_options: FetchOptions,
5664        askpass: AskPassDelegate,
5665        _cx: &mut App,
5666    ) -> oneshot::Receiver<Result<RemoteCommandOutput>> {
5667        let askpass_delegates = self.askpass_delegates.clone();
5668        let askpass_id = util::post_inc(&mut self.latest_askpass_id);
5669        let id = self.id;
5670
5671        self.send_job(Some("git fetch".into()), move |git_repo, cx| async move {
5672            match git_repo {
5673                RepositoryState::Local(LocalRepositoryState {
5674                    backend,
5675                    environment,
5676                    ..
5677                }) => backend.fetch(fetch_options, askpass, environment, cx).await,
5678                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5679                    askpass_delegates.lock().insert(askpass_id, askpass);
5680                    let _defer = util::defer(|| {
5681                        let askpass_delegate = askpass_delegates.lock().remove(&askpass_id);
5682                        debug_assert!(askpass_delegate.is_some());
5683                    });
5684
5685                    let response = client
5686                        .request(proto::Fetch {
5687                            project_id: project_id.0,
5688                            repository_id: id.to_proto(),
5689                            askpass_id,
5690                            remote: fetch_options.to_proto(),
5691                        })
5692                        .await?;
5693
5694                    Ok(RemoteCommandOutput {
5695                        stdout: response.stdout,
5696                        stderr: response.stderr,
5697                    })
5698                }
5699            }
5700        })
5701    }
5702
5703    pub fn push(
5704        &mut self,
5705        branch: SharedString,
5706        remote_branch: SharedString,
5707        remote: SharedString,
5708        options: Option<PushOptions>,
5709        askpass: AskPassDelegate,
5710        cx: &mut Context<Self>,
5711    ) -> oneshot::Receiver<Result<RemoteCommandOutput>> {
5712        let askpass_delegates = self.askpass_delegates.clone();
5713        let askpass_id = util::post_inc(&mut self.latest_askpass_id);
5714        let id = self.id;
5715
5716        let args = options
5717            .map(|option| match option {
5718                PushOptions::SetUpstream => " --set-upstream",
5719                PushOptions::Force => " --force-with-lease",
5720            })
5721            .unwrap_or("");
5722
5723        let updates_tx = self
5724            .git_store()
5725            .and_then(|git_store| match &git_store.read(cx).state {
5726                GitStoreState::Local { downstream, .. } => downstream
5727                    .as_ref()
5728                    .map(|downstream| downstream.updates_tx.clone()),
5729                _ => None,
5730            });
5731
5732        let this = cx.weak_entity();
5733        self.send_job(
5734            Some(format!("git push {} {} {}:{}", args, remote, branch, remote_branch).into()),
5735            move |git_repo, mut cx| async move {
5736                match git_repo {
5737                    RepositoryState::Local(LocalRepositoryState {
5738                        backend,
5739                        environment,
5740                        ..
5741                    }) => {
5742                        let result = backend
5743                            .push(
5744                                branch.to_string(),
5745                                remote_branch.to_string(),
5746                                remote.to_string(),
5747                                options,
5748                                askpass,
5749                                environment.clone(),
5750                                cx.clone(),
5751                            )
5752                            .await;
5753                        // TODO would be nice to not have to do this manually
5754                        if result.is_ok() {
5755                            let branches = backend.branches().await?;
5756                            let branch = branches.into_iter().find(|branch| branch.is_head);
5757                            log::info!("head branch after scan is {branch:?}");
5758                            let snapshot = this.update(&mut cx, |this, cx| {
5759                                this.snapshot.branch = branch;
5760                                cx.emit(RepositoryEvent::HeadChanged);
5761                                this.snapshot.clone()
5762                            })?;
5763                            if let Some(updates_tx) = updates_tx {
5764                                updates_tx
5765                                    .unbounded_send(DownstreamUpdate::UpdateRepository(snapshot))
5766                                    .ok();
5767                            }
5768                        }
5769                        result
5770                    }
5771                    RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5772                        askpass_delegates.lock().insert(askpass_id, askpass);
5773                        let _defer = util::defer(|| {
5774                            let askpass_delegate = askpass_delegates.lock().remove(&askpass_id);
5775                            debug_assert!(askpass_delegate.is_some());
5776                        });
5777                        let response = client
5778                            .request(proto::Push {
5779                                project_id: project_id.0,
5780                                repository_id: id.to_proto(),
5781                                askpass_id,
5782                                branch_name: branch.to_string(),
5783                                remote_branch_name: remote_branch.to_string(),
5784                                remote_name: remote.to_string(),
5785                                options: options.map(|options| match options {
5786                                    PushOptions::Force => proto::push::PushOptions::Force,
5787                                    PushOptions::SetUpstream => {
5788                                        proto::push::PushOptions::SetUpstream
5789                                    }
5790                                }
5791                                    as i32),
5792                            })
5793                            .await?;
5794
5795                        Ok(RemoteCommandOutput {
5796                            stdout: response.stdout,
5797                            stderr: response.stderr,
5798                        })
5799                    }
5800                }
5801            },
5802        )
5803    }
5804
5805    pub fn pull(
5806        &mut self,
5807        branch: Option<SharedString>,
5808        remote: SharedString,
5809        rebase: bool,
5810        askpass: AskPassDelegate,
5811        _cx: &mut App,
5812    ) -> oneshot::Receiver<Result<RemoteCommandOutput>> {
5813        let askpass_delegates = self.askpass_delegates.clone();
5814        let askpass_id = util::post_inc(&mut self.latest_askpass_id);
5815        let id = self.id;
5816
5817        let mut status = "git pull".to_string();
5818        if rebase {
5819            status.push_str(" --rebase");
5820        }
5821        status.push_str(&format!(" {}", remote));
5822        if let Some(b) = &branch {
5823            status.push_str(&format!(" {}", b));
5824        }
5825
5826        self.send_job(Some(status.into()), move |git_repo, cx| async move {
5827            match git_repo {
5828                RepositoryState::Local(LocalRepositoryState {
5829                    backend,
5830                    environment,
5831                    ..
5832                }) => {
5833                    backend
5834                        .pull(
5835                            branch.as_ref().map(|b| b.to_string()),
5836                            remote.to_string(),
5837                            rebase,
5838                            askpass,
5839                            environment.clone(),
5840                            cx,
5841                        )
5842                        .await
5843                }
5844                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5845                    askpass_delegates.lock().insert(askpass_id, askpass);
5846                    let _defer = util::defer(|| {
5847                        let askpass_delegate = askpass_delegates.lock().remove(&askpass_id);
5848                        debug_assert!(askpass_delegate.is_some());
5849                    });
5850                    let response = client
5851                        .request(proto::Pull {
5852                            project_id: project_id.0,
5853                            repository_id: id.to_proto(),
5854                            askpass_id,
5855                            rebase,
5856                            branch_name: branch.as_ref().map(|b| b.to_string()),
5857                            remote_name: remote.to_string(),
5858                        })
5859                        .await?;
5860
5861                    Ok(RemoteCommandOutput {
5862                        stdout: response.stdout,
5863                        stderr: response.stderr,
5864                    })
5865                }
5866            }
5867        })
5868    }
5869
5870    fn spawn_set_index_text_job(
5871        &mut self,
5872        path: RepoPath,
5873        content: Option<String>,
5874        hunk_staging_operation_count: Option<usize>,
5875        cx: &mut Context<Self>,
5876    ) -> oneshot::Receiver<anyhow::Result<()>> {
5877        let id = self.id;
5878        let this = cx.weak_entity();
5879        let git_store = self.git_store.clone();
5880        let abs_path = self.snapshot.repo_path_to_abs_path(&path);
5881        self.send_keyed_job(
5882            Some(GitJobKey::WriteIndex(vec![path.clone()])),
5883            None,
5884            move |git_repo, mut cx| async move {
5885                log::debug!(
5886                    "start updating index text for buffer {}",
5887                    path.as_unix_str()
5888                );
5889
5890                match git_repo {
5891                    RepositoryState::Local(LocalRepositoryState {
5892                        fs,
5893                        backend,
5894                        environment,
5895                        ..
5896                    }) => {
5897                        let executable = match fs.metadata(&abs_path).await {
5898                            Ok(Some(meta)) => meta.is_executable,
5899                            Ok(None) => false,
5900                            Err(_err) => false,
5901                        };
5902                        backend
5903                            .set_index_text(path.clone(), content, environment.clone(), executable)
5904                            .await?;
5905                    }
5906                    RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5907                        client
5908                            .request(proto::SetIndexText {
5909                                project_id: project_id.0,
5910                                repository_id: id.to_proto(),
5911                                path: path.to_proto(),
5912                                text: content,
5913                            })
5914                            .await?;
5915                    }
5916                }
5917                log::debug!(
5918                    "finish updating index text for buffer {}",
5919                    path.as_unix_str()
5920                );
5921
5922                if let Some(hunk_staging_operation_count) = hunk_staging_operation_count {
5923                    let project_path = this
5924                        .read_with(&cx, |this, cx| this.repo_path_to_project_path(&path, cx))
5925                        .ok()
5926                        .flatten();
5927                    git_store
5928                        .update(&mut cx, |git_store, cx| {
5929                            let buffer_id = git_store
5930                                .buffer_store
5931                                .read(cx)
5932                                .get_by_path(&project_path?)?
5933                                .read(cx)
5934                                .remote_id();
5935                            let diff_state = git_store.diffs.get(&buffer_id)?;
5936                            diff_state.update(cx, |diff_state, _| {
5937                                diff_state.hunk_staging_operation_count_as_of_write =
5938                                    hunk_staging_operation_count;
5939                            });
5940                            Some(())
5941                        })
5942                        .context("Git store dropped")?;
5943                }
5944                Ok(())
5945            },
5946        )
5947    }
5948
5949    pub fn create_remote(
5950        &mut self,
5951        remote_name: String,
5952        remote_url: String,
5953    ) -> oneshot::Receiver<Result<()>> {
5954        let id = self.id;
5955        self.send_job(
5956            Some(format!("git remote add {remote_name} {remote_url}").into()),
5957            move |repo, _cx| async move {
5958                match repo {
5959                    RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
5960                        backend.create_remote(remote_name, remote_url).await
5961                    }
5962                    RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5963                        client
5964                            .request(proto::GitCreateRemote {
5965                                project_id: project_id.0,
5966                                repository_id: id.to_proto(),
5967                                remote_name,
5968                                remote_url,
5969                            })
5970                            .await?;
5971
5972                        Ok(())
5973                    }
5974                }
5975            },
5976        )
5977    }
5978
5979    pub fn remove_remote(&mut self, remote_name: String) -> oneshot::Receiver<Result<()>> {
5980        let id = self.id;
5981        self.send_job(
5982            Some(format!("git remove remote {remote_name}").into()),
5983            move |repo, _cx| async move {
5984                match repo {
5985                    RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
5986                        backend.remove_remote(remote_name).await
5987                    }
5988                    RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
5989                        client
5990                            .request(proto::GitRemoveRemote {
5991                                project_id: project_id.0,
5992                                repository_id: id.to_proto(),
5993                                remote_name,
5994                            })
5995                            .await?;
5996
5997                        Ok(())
5998                    }
5999                }
6000            },
6001        )
6002    }
6003
6004    pub fn get_remotes(
6005        &mut self,
6006        branch_name: Option<String>,
6007        is_push: bool,
6008    ) -> oneshot::Receiver<Result<Vec<Remote>>> {
6009        let id = self.id;
6010        self.send_job(None, move |repo, _cx| async move {
6011            match repo {
6012                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6013                    let remote = if let Some(branch_name) = branch_name {
6014                        if is_push {
6015                            backend.get_push_remote(branch_name).await?
6016                        } else {
6017                            backend.get_branch_remote(branch_name).await?
6018                        }
6019                    } else {
6020                        None
6021                    };
6022
6023                    match remote {
6024                        Some(remote) => Ok(vec![remote]),
6025                        None => backend.get_all_remotes().await,
6026                    }
6027                }
6028                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6029                    let response = client
6030                        .request(proto::GetRemotes {
6031                            project_id: project_id.0,
6032                            repository_id: id.to_proto(),
6033                            branch_name,
6034                            is_push,
6035                        })
6036                        .await?;
6037
6038                    let remotes = response
6039                        .remotes
6040                        .into_iter()
6041                        .map(|remotes| Remote {
6042                            name: remotes.name.into(),
6043                        })
6044                        .collect();
6045
6046                    Ok(remotes)
6047                }
6048            }
6049        })
6050    }
6051
6052    pub fn branches(&mut self) -> oneshot::Receiver<Result<Vec<Branch>>> {
6053        let id = self.id;
6054        self.send_job(None, move |repo, _| async move {
6055            match repo {
6056                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6057                    backend.branches().await
6058                }
6059                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6060                    let response = client
6061                        .request(proto::GitGetBranches {
6062                            project_id: project_id.0,
6063                            repository_id: id.to_proto(),
6064                        })
6065                        .await?;
6066
6067                    let branches = response
6068                        .branches
6069                        .into_iter()
6070                        .map(|branch| proto_to_branch(&branch))
6071                        .collect();
6072
6073                    Ok(branches)
6074                }
6075            }
6076        })
6077    }
6078
6079    /// If this is a linked worktree (*NOT* the main checkout of a repository),
6080    /// returns the pathed for the linked worktree.
6081    ///
6082    /// Returns None if this is the main checkout.
6083    pub fn linked_worktree_path(&self) -> Option<&Arc<Path>> {
6084        if self.work_directory_abs_path != self.original_repo_abs_path {
6085            Some(&self.work_directory_abs_path)
6086        } else {
6087            None
6088        }
6089    }
6090
6091    pub fn path_for_new_linked_worktree(
6092        &self,
6093        branch_name: &str,
6094        worktree_directory_setting: &str,
6095    ) -> Result<PathBuf> {
6096        let original_repo = self.original_repo_abs_path.clone();
6097        let project_name = original_repo
6098            .file_name()
6099            .ok_or_else(|| anyhow!("git repo must have a directory name"))?;
6100        let directory = worktrees_directory_for_repo(&original_repo, worktree_directory_setting)?;
6101        Ok(directory.join(branch_name).join(project_name))
6102    }
6103
6104    pub fn worktrees(&mut self) -> oneshot::Receiver<Result<Vec<GitWorktree>>> {
6105        let id = self.id;
6106        self.send_job(None, move |repo, _| async move {
6107            match repo {
6108                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6109                    backend.worktrees().await
6110                }
6111                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6112                    let response = client
6113                        .request(proto::GitGetWorktrees {
6114                            project_id: project_id.0,
6115                            repository_id: id.to_proto(),
6116                        })
6117                        .await?;
6118
6119                    let worktrees = response
6120                        .worktrees
6121                        .into_iter()
6122                        .map(|worktree| proto_to_worktree(&worktree))
6123                        .collect();
6124
6125                    Ok(worktrees)
6126                }
6127            }
6128        })
6129    }
6130
6131    pub fn create_worktree(
6132        &mut self,
6133        target: CreateWorktreeTarget,
6134        path: PathBuf,
6135    ) -> oneshot::Receiver<Result<()>> {
6136        let id = self.id;
6137        let job_description = match target.branch_name() {
6138            Some(branch_name) => format!("git worktree add: {branch_name}"),
6139            None => "git worktree add (detached)".to_string(),
6140        };
6141        self.send_job(Some(job_description.into()), move |repo, _cx| async move {
6142            match repo {
6143                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6144                    backend.create_worktree(target, path).await
6145                }
6146                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6147                    let (name, commit, use_existing_branch) = match target {
6148                        CreateWorktreeTarget::ExistingBranch { branch_name } => {
6149                            (Some(branch_name), None, true)
6150                        }
6151                        CreateWorktreeTarget::NewBranch {
6152                            branch_name,
6153                            base_sha,
6154                        } => (Some(branch_name), base_sha, false),
6155                        CreateWorktreeTarget::Detached { base_sha } => (None, base_sha, false),
6156                    };
6157
6158                    client
6159                        .request(proto::GitCreateWorktree {
6160                            project_id: project_id.0,
6161                            repository_id: id.to_proto(),
6162                            name: name.unwrap_or_default(),
6163                            directory: path.to_string_lossy().to_string(),
6164                            commit,
6165                            use_existing_branch,
6166                        })
6167                        .await?;
6168
6169                    Ok(())
6170                }
6171            }
6172        })
6173    }
6174
6175    pub fn create_worktree_detached(
6176        &mut self,
6177        path: PathBuf,
6178        commit: String,
6179    ) -> oneshot::Receiver<Result<()>> {
6180        self.create_worktree(
6181            CreateWorktreeTarget::Detached {
6182                base_sha: Some(commit),
6183            },
6184            path,
6185        )
6186    }
6187
6188    pub fn checkout_branch_in_worktree(
6189        &mut self,
6190        branch_name: String,
6191        worktree_path: PathBuf,
6192        create: bool,
6193    ) -> oneshot::Receiver<Result<()>> {
6194        let description = if create {
6195            format!("git checkout -b {branch_name}")
6196        } else {
6197            format!("git checkout {branch_name}")
6198        };
6199        self.send_job(Some(description.into()), move |repo, _cx| async move {
6200            match repo {
6201                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6202                    backend
6203                        .checkout_branch_in_worktree(branch_name, worktree_path, create)
6204                        .await
6205                }
6206                RepositoryState::Remote(_) => {
6207                    log::warn!("checkout_branch_in_worktree not supported for remote repositories");
6208                    Ok(())
6209                }
6210            }
6211        })
6212    }
6213
6214    pub fn head_sha(&mut self) -> oneshot::Receiver<Result<Option<String>>> {
6215        let id = self.id;
6216        self.send_job(None, move |repo, _cx| async move {
6217            match repo {
6218                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6219                    Ok(backend.head_sha().await)
6220                }
6221                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6222                    let response = client
6223                        .request(proto::GitGetHeadSha {
6224                            project_id: project_id.0,
6225                            repository_id: id.to_proto(),
6226                        })
6227                        .await?;
6228
6229                    Ok(response.sha)
6230                }
6231            }
6232        })
6233    }
6234
6235    fn edit_ref(
6236        &mut self,
6237        ref_name: String,
6238        commit: Option<String>,
6239    ) -> oneshot::Receiver<Result<()>> {
6240        let id = self.id;
6241        self.send_job(None, move |repo, _cx| async move {
6242            match repo {
6243                RepositoryState::Local(LocalRepositoryState { backend, .. }) => match commit {
6244                    Some(commit) => backend.update_ref(ref_name, commit).await,
6245                    None => backend.delete_ref(ref_name).await,
6246                },
6247                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6248                    let action = match commit {
6249                        Some(sha) => proto::git_edit_ref::Action::UpdateToCommit(sha),
6250                        None => {
6251                            proto::git_edit_ref::Action::Delete(proto::git_edit_ref::DeleteRef {})
6252                        }
6253                    };
6254                    client
6255                        .request(proto::GitEditRef {
6256                            project_id: project_id.0,
6257                            repository_id: id.to_proto(),
6258                            ref_name,
6259                            action: Some(action),
6260                        })
6261                        .await?;
6262                    Ok(())
6263                }
6264            }
6265        })
6266    }
6267
6268    pub fn update_ref(
6269        &mut self,
6270        ref_name: String,
6271        commit: String,
6272    ) -> oneshot::Receiver<Result<()>> {
6273        self.edit_ref(ref_name, Some(commit))
6274    }
6275
6276    pub fn delete_ref(&mut self, ref_name: String) -> oneshot::Receiver<Result<()>> {
6277        self.edit_ref(ref_name, None)
6278    }
6279
6280    pub fn repair_worktrees(&mut self) -> oneshot::Receiver<Result<()>> {
6281        let id = self.id;
6282        self.send_job(None, move |repo, _cx| async move {
6283            match repo {
6284                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6285                    backend.repair_worktrees().await
6286                }
6287                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6288                    client
6289                        .request(proto::GitRepairWorktrees {
6290                            project_id: project_id.0,
6291                            repository_id: id.to_proto(),
6292                        })
6293                        .await?;
6294                    Ok(())
6295                }
6296            }
6297        })
6298    }
6299
6300    pub fn create_archive_checkpoint(&mut self) -> oneshot::Receiver<Result<(String, String)>> {
6301        let id = self.id;
6302        self.send_job(None, move |repo, _cx| async move {
6303            match repo {
6304                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6305                    backend.create_archive_checkpoint().await
6306                }
6307                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6308                    let response = client
6309                        .request(proto::GitCreateArchiveCheckpoint {
6310                            project_id: project_id.0,
6311                            repository_id: id.to_proto(),
6312                        })
6313                        .await?;
6314                    Ok((response.staged_commit_sha, response.unstaged_commit_sha))
6315                }
6316            }
6317        })
6318    }
6319
6320    pub fn restore_archive_checkpoint(
6321        &mut self,
6322        staged_sha: String,
6323        unstaged_sha: String,
6324    ) -> oneshot::Receiver<Result<()>> {
6325        let id = self.id;
6326        self.send_job(None, move |repo, _cx| async move {
6327            match repo {
6328                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6329                    backend
6330                        .restore_archive_checkpoint(staged_sha, unstaged_sha)
6331                        .await
6332                }
6333                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6334                    client
6335                        .request(proto::GitRestoreArchiveCheckpoint {
6336                            project_id: project_id.0,
6337                            repository_id: id.to_proto(),
6338                            staged_commit_sha: staged_sha,
6339                            unstaged_commit_sha: unstaged_sha,
6340                        })
6341                        .await?;
6342                    Ok(())
6343                }
6344            }
6345        })
6346    }
6347
6348    pub fn remove_worktree(&mut self, path: PathBuf, force: bool) -> oneshot::Receiver<Result<()>> {
6349        let id = self.id;
6350        let original_repo_abs_path = self.snapshot.original_repo_abs_path.clone();
6351        self.send_job(
6352            Some(format!("git worktree remove: {}", path.display()).into()),
6353            move |repo, cx| async move {
6354                match repo {
6355                    RepositoryState::Local(LocalRepositoryState { backend, fs, .. }) => {
6356                        // When forcing, delete the worktree directory ourselves before
6357                        // invoking git. `git worktree remove` can remove the admin
6358                        // metadata in `.git/worktrees/<name>` but fail to delete the
6359                        // working directory (it continues past directory-removal errors),
6360                        // leaving an orphaned folder on disk. Deleting first guarantees
6361                        // the directory is gone, and `git worktree remove --force`
6362                        // tolerates a missing working tree while cleaning up the admin
6363                        // entry. We keep this inside the `Local` arm so that for remote
6364                        // projects the deletion runs on the remote machine (where the
6365                        // `GitRemoveWorktree` RPC is handled against the local repo on
6366                        // the headless server) using its own filesystem.
6367                        //
6368                        // After a successful removal, also delete any empty ancestor
6369                        // directories between the worktree path and the configured
6370                        // base directory used when creating linked worktrees.
6371                        //
6372                        // Non-force removals are left untouched before git runs:
6373                        // `git worktree remove` must see the dirty working tree to
6374                        // refuse the operation.
6375                        if force {
6376                            fs.remove_dir(
6377                                &path,
6378                                RemoveOptions {
6379                                    recursive: true,
6380                                    ignore_if_not_exists: true,
6381                                },
6382                            )
6383                            .await
6384                            .with_context(|| {
6385                                format!("failed to delete worktree directory '{}'", path.display())
6386                            })?;
6387                        }
6388
6389                        backend.remove_worktree(path.clone(), force).await?;
6390
6391                        let managed_worktree_base = cx.update(|cx| {
6392                            let setting = &ProjectSettings::get_global(cx).git.worktree_directory;
6393                            worktrees_directory_for_repo(&original_repo_abs_path, setting).log_err()
6394                        });
6395
6396                        if let Some(managed_worktree_base) = managed_worktree_base {
6397                            remove_empty_managed_worktree_ancestors(
6398                                fs.as_ref(),
6399                                &path,
6400                                &managed_worktree_base,
6401                            )
6402                            .await;
6403                        }
6404
6405                        Ok(())
6406                    }
6407                    RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6408                        client
6409                            .request(proto::GitRemoveWorktree {
6410                                project_id: project_id.0,
6411                                repository_id: id.to_proto(),
6412                                path: path.to_string_lossy().to_string(),
6413                                force,
6414                            })
6415                            .await?;
6416
6417                        Ok(())
6418                    }
6419                }
6420            },
6421        )
6422    }
6423
6424    pub fn rename_worktree(
6425        &mut self,
6426        old_path: PathBuf,
6427        new_path: PathBuf,
6428    ) -> oneshot::Receiver<Result<()>> {
6429        let id = self.id;
6430        self.send_job(
6431            Some(format!("git worktree move: {}", old_path.display()).into()),
6432            move |repo, _cx| async move {
6433                match repo {
6434                    RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6435                        backend.rename_worktree(old_path, new_path).await
6436                    }
6437                    RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6438                        client
6439                            .request(proto::GitRenameWorktree {
6440                                project_id: project_id.0,
6441                                repository_id: id.to_proto(),
6442                                old_path: old_path.to_string_lossy().to_string(),
6443                                new_path: new_path.to_string_lossy().to_string(),
6444                            })
6445                            .await?;
6446
6447                        Ok(())
6448                    }
6449                }
6450            },
6451        )
6452    }
6453
6454    pub fn default_branch(
6455        &mut self,
6456        include_remote_name: bool,
6457    ) -> oneshot::Receiver<Result<Option<SharedString>>> {
6458        let id = self.id;
6459        self.send_job(None, move |repo, _| async move {
6460            match repo {
6461                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6462                    backend.default_branch(include_remote_name).await
6463                }
6464                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6465                    let response = client
6466                        .request(proto::GetDefaultBranch {
6467                            project_id: project_id.0,
6468                            repository_id: id.to_proto(),
6469                        })
6470                        .await?;
6471
6472                    anyhow::Ok(response.branch.map(SharedString::from))
6473                }
6474            }
6475        })
6476    }
6477
6478    pub fn diff_tree(
6479        &mut self,
6480        diff_type: DiffTreeType,
6481        _cx: &App,
6482    ) -> oneshot::Receiver<Result<TreeDiff>> {
6483        let repository_id = self.snapshot.id;
6484        self.send_job(None, move |repo, _cx| async move {
6485            match repo {
6486                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6487                    backend.diff_tree(diff_type).await
6488                }
6489                RepositoryState::Remote(RemoteRepositoryState { client, project_id }) => {
6490                    let response = client
6491                        .request(proto::GetTreeDiff {
6492                            project_id: project_id.0,
6493                            repository_id: repository_id.0,
6494                            is_merge: matches!(diff_type, DiffTreeType::MergeBase { .. }),
6495                            base: diff_type.base().to_string(),
6496                            head: diff_type.head().to_string(),
6497                        })
6498                        .await?;
6499
6500                    let entries = response
6501                        .entries
6502                        .into_iter()
6503                        .filter_map(|entry| {
6504                            let status = match entry.status() {
6505                                proto::tree_diff_status::Status::Added => TreeDiffStatus::Added,
6506                                proto::tree_diff_status::Status::Modified => {
6507                                    TreeDiffStatus::Modified {
6508                                        old: git::Oid::from_str(
6509                                            &entry.oid.context("missing oid").log_err()?,
6510                                        )
6511                                        .log_err()?,
6512                                    }
6513                                }
6514                                proto::tree_diff_status::Status::Deleted => {
6515                                    TreeDiffStatus::Deleted {
6516                                        old: git::Oid::from_str(
6517                                            &entry.oid.context("missing oid").log_err()?,
6518                                        )
6519                                        .log_err()?,
6520                                    }
6521                                }
6522                            };
6523                            Some((
6524                                RepoPath::from_rel_path(
6525                                    &RelPath::from_proto(&entry.path).log_err()?,
6526                                ),
6527                                status,
6528                            ))
6529                        })
6530                        .collect();
6531
6532                    Ok(TreeDiff { entries })
6533                }
6534            }
6535        })
6536    }
6537
6538    pub fn diff(&mut self, diff_type: DiffType, _cx: &App) -> oneshot::Receiver<Result<String>> {
6539        let id = self.id;
6540        self.send_job(None, move |repo, _cx| async move {
6541            match repo {
6542                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6543                    backend.diff(diff_type).await
6544                }
6545                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6546                    let (proto_diff_type, merge_base_ref) = match &diff_type {
6547                        DiffType::HeadToIndex => {
6548                            (proto::git_diff::DiffType::HeadToIndex.into(), None)
6549                        }
6550                        DiffType::HeadToWorktree => {
6551                            (proto::git_diff::DiffType::HeadToWorktree.into(), None)
6552                        }
6553                        DiffType::MergeBase { base_ref } => (
6554                            proto::git_diff::DiffType::MergeBase.into(),
6555                            Some(base_ref.to_string()),
6556                        ),
6557                    };
6558                    let response = client
6559                        .request(proto::GitDiff {
6560                            project_id: project_id.0,
6561                            repository_id: id.to_proto(),
6562                            diff_type: proto_diff_type,
6563                            merge_base_ref,
6564                        })
6565                        .await?;
6566
6567                    Ok(response.diff)
6568                }
6569            }
6570        })
6571    }
6572
6573    pub fn create_branch(
6574        &mut self,
6575        branch_name: String,
6576        base_branch: Option<String>,
6577    ) -> oneshot::Receiver<Result<()>> {
6578        let id = self.id;
6579        let status_msg = if let Some(ref base) = base_branch {
6580            format!("git switch -c {branch_name} {base}").into()
6581        } else {
6582            format!("git switch -c {branch_name}").into()
6583        };
6584        self.send_job(Some(status_msg), move |repo, _cx| async move {
6585            match repo {
6586                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6587                    backend.create_branch(branch_name, base_branch).await
6588                }
6589                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6590                    client
6591                        .request(proto::GitCreateBranch {
6592                            project_id: project_id.0,
6593                            repository_id: id.to_proto(),
6594                            branch_name,
6595                        })
6596                        .await?;
6597
6598                    Ok(())
6599                }
6600            }
6601        })
6602    }
6603
6604    pub fn change_branch(&mut self, branch_name: String) -> oneshot::Receiver<Result<()>> {
6605        let id = self.id;
6606        self.send_job(
6607            Some(format!("git switch {branch_name}").into()),
6608            move |repo, _cx| async move {
6609                match repo {
6610                    RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6611                        backend.change_branch(branch_name).await
6612                    }
6613                    RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6614                        client
6615                            .request(proto::GitChangeBranch {
6616                                project_id: project_id.0,
6617                                repository_id: id.to_proto(),
6618                                branch_name,
6619                            })
6620                            .await?;
6621
6622                        Ok(())
6623                    }
6624                }
6625            },
6626        )
6627    }
6628
6629    pub fn delete_branch(
6630        &mut self,
6631        is_remote: bool,
6632        branch_name: String,
6633    ) -> oneshot::Receiver<Result<()>> {
6634        let id = self.id;
6635        self.send_job(
6636            Some(
6637                format!(
6638                    "git branch {} {}",
6639                    if is_remote { "-dr" } else { "-d" },
6640                    branch_name
6641                )
6642                .into(),
6643            ),
6644            move |repo, _cx| async move {
6645                match repo {
6646                    RepositoryState::Local(state) => {
6647                        state.backend.delete_branch(is_remote, branch_name).await
6648                    }
6649                    RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6650                        client
6651                            .request(proto::GitDeleteBranch {
6652                                project_id: project_id.0,
6653                                repository_id: id.to_proto(),
6654                                is_remote,
6655                                branch_name,
6656                            })
6657                            .await?;
6658
6659                        Ok(())
6660                    }
6661                }
6662            },
6663        )
6664    }
6665
6666    pub fn rename_branch(
6667        &mut self,
6668        branch: String,
6669        new_name: String,
6670    ) -> oneshot::Receiver<Result<()>> {
6671        let id = self.id;
6672        self.send_job(
6673            Some(format!("git branch -m {branch} {new_name}").into()),
6674            move |repo, _cx| async move {
6675                match repo {
6676                    RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6677                        backend.rename_branch(branch, new_name).await
6678                    }
6679                    RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6680                        client
6681                            .request(proto::GitRenameBranch {
6682                                project_id: project_id.0,
6683                                repository_id: id.to_proto(),
6684                                branch,
6685                                new_name,
6686                            })
6687                            .await?;
6688
6689                        Ok(())
6690                    }
6691                }
6692            },
6693        )
6694    }
6695
6696    pub fn check_for_pushed_commits(&mut self) -> oneshot::Receiver<Result<Vec<SharedString>>> {
6697        let id = self.id;
6698        self.send_job(None, move |repo, _cx| async move {
6699            match repo {
6700                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6701                    backend.check_for_pushed_commit().await
6702                }
6703                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6704                    let response = client
6705                        .request(proto::CheckForPushedCommits {
6706                            project_id: project_id.0,
6707                            repository_id: id.to_proto(),
6708                        })
6709                        .await?;
6710
6711                    let branches = response.pushed_to.into_iter().map(Into::into).collect();
6712
6713                    Ok(branches)
6714                }
6715            }
6716        })
6717    }
6718
6719    pub fn checkpoint(&mut self) -> oneshot::Receiver<Result<GitRepositoryCheckpoint>> {
6720        let id = self.id;
6721        self.send_job(None, move |repo, _cx| async move {
6722            match repo {
6723                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6724                    backend.checkpoint().await
6725                }
6726                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6727                    let response = client
6728                        .request(proto::GitCreateCheckpoint {
6729                            project_id: project_id.0,
6730                            repository_id: id.to_proto(),
6731                        })
6732                        .await?;
6733
6734                    Ok(GitRepositoryCheckpoint {
6735                        commit_sha: Oid::from_bytes(&response.commit_sha)?,
6736                    })
6737                }
6738            }
6739        })
6740    }
6741
6742    pub fn restore_checkpoint(
6743        &mut self,
6744        checkpoint: GitRepositoryCheckpoint,
6745    ) -> oneshot::Receiver<Result<()>> {
6746        let id = self.id;
6747        self.send_job(None, move |repo, _cx| async move {
6748            match repo {
6749                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6750                    backend.restore_checkpoint(checkpoint).await
6751                }
6752                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6753                    client
6754                        .request(proto::GitRestoreCheckpoint {
6755                            project_id: project_id.0,
6756                            repository_id: id.to_proto(),
6757                            commit_sha: checkpoint.commit_sha.as_bytes().to_vec(),
6758                        })
6759                        .await?;
6760                    Ok(())
6761                }
6762            }
6763        })
6764    }
6765
6766    pub(crate) fn apply_remote_update(
6767        &mut self,
6768        update: proto::UpdateRepository,
6769        cx: &mut Context<Self>,
6770    ) -> Result<()> {
6771        if let Some(main_path) = &update.original_repo_abs_path {
6772            self.snapshot.original_repo_abs_path = Path::new(main_path.as_str()).into();
6773        }
6774
6775        let new_branch = update.branch_summary.as_ref().map(proto_to_branch);
6776        let new_head_commit = update
6777            .head_commit_details
6778            .as_ref()
6779            .map(proto_to_commit_details);
6780        if self.snapshot.branch != new_branch || self.snapshot.head_commit != new_head_commit {
6781            cx.emit(RepositoryEvent::HeadChanged)
6782        }
6783        self.snapshot.branch = new_branch;
6784        self.snapshot.head_commit = new_head_commit;
6785
6786        // We don't store any merge head state for downstream projects; the upstream
6787        // will track it and we will just get the updated conflicts
6788        let new_merge_heads = TreeMap::from_ordered_entries(
6789            update
6790                .current_merge_conflicts
6791                .into_iter()
6792                .filter_map(|path| Some((RepoPath::from_proto(&path).ok()?, vec![]))),
6793        );
6794        let conflicts_changed =
6795            self.snapshot.merge.merge_heads_by_conflicted_path != new_merge_heads;
6796        self.snapshot.merge.merge_heads_by_conflicted_path = new_merge_heads;
6797        self.snapshot.merge.message = update.merge_message.map(SharedString::from);
6798        let new_stash_entries = GitStash {
6799            entries: update
6800                .stash_entries
6801                .iter()
6802                .filter_map(|entry| proto_to_stash(entry).ok())
6803                .collect(),
6804        };
6805        if self.snapshot.stash_entries != new_stash_entries {
6806            cx.emit(RepositoryEvent::StashEntriesChanged)
6807        }
6808        self.snapshot.stash_entries = new_stash_entries;
6809        let new_linked_worktrees: Arc<[GitWorktree]> = update
6810            .linked_worktrees
6811            .iter()
6812            .map(proto_to_worktree)
6813            .collect();
6814        if *self.snapshot.linked_worktrees != *new_linked_worktrees {
6815            cx.emit(RepositoryEvent::GitWorktreeListChanged);
6816        }
6817        self.snapshot.linked_worktrees = new_linked_worktrees;
6818        self.snapshot.remote_upstream_url = update.remote_upstream_url;
6819        self.snapshot.remote_origin_url = update.remote_origin_url;
6820
6821        let edits = update
6822            .removed_statuses
6823            .into_iter()
6824            .filter_map(|path| {
6825                Some(sum_tree::Edit::Remove(PathKey(
6826                    RelPath::from_proto(&path).log_err()?,
6827                )))
6828            })
6829            .chain(
6830                update
6831                    .updated_statuses
6832                    .into_iter()
6833                    .filter_map(|updated_status| {
6834                        Some(sum_tree::Edit::Insert(updated_status.try_into().log_err()?))
6835                    }),
6836            )
6837            .collect::<Vec<_>>();
6838        if conflicts_changed || !edits.is_empty() {
6839            cx.emit(RepositoryEvent::StatusesChanged);
6840        }
6841        self.snapshot.statuses_by_path.edit(edits, ());
6842
6843        if update.is_last_update {
6844            self.snapshot.scan_id = update.scan_id;
6845        }
6846        self.clear_pending_ops(cx);
6847        Ok(())
6848    }
6849
6850    pub fn compare_checkpoints(
6851        &mut self,
6852        left: GitRepositoryCheckpoint,
6853        right: GitRepositoryCheckpoint,
6854    ) -> oneshot::Receiver<Result<bool>> {
6855        let id = self.id;
6856        self.send_job(None, move |repo, _cx| async move {
6857            match repo {
6858                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6859                    backend.compare_checkpoints(left, right).await
6860                }
6861                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6862                    let response = client
6863                        .request(proto::GitCompareCheckpoints {
6864                            project_id: project_id.0,
6865                            repository_id: id.to_proto(),
6866                            left_commit_sha: left.commit_sha.as_bytes().to_vec(),
6867                            right_commit_sha: right.commit_sha.as_bytes().to_vec(),
6868                        })
6869                        .await?;
6870                    Ok(response.equal)
6871                }
6872            }
6873        })
6874    }
6875
6876    pub fn diff_checkpoints(
6877        &mut self,
6878        base_checkpoint: GitRepositoryCheckpoint,
6879        target_checkpoint: GitRepositoryCheckpoint,
6880    ) -> oneshot::Receiver<Result<String>> {
6881        let id = self.id;
6882        self.send_job(None, move |repo, _cx| async move {
6883            match repo {
6884                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
6885                    backend
6886                        .diff_checkpoints(base_checkpoint, target_checkpoint)
6887                        .await
6888                }
6889                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
6890                    let response = client
6891                        .request(proto::GitDiffCheckpoints {
6892                            project_id: project_id.0,
6893                            repository_id: id.to_proto(),
6894                            base_commit_sha: base_checkpoint.commit_sha.as_bytes().to_vec(),
6895                            target_commit_sha: target_checkpoint.commit_sha.as_bytes().to_vec(),
6896                        })
6897                        .await?;
6898                    Ok(response.diff)
6899                }
6900            }
6901        })
6902    }
6903
6904    fn clear_pending_ops(&mut self, cx: &mut Context<Self>) {
6905        let updated = SumTree::from_iter(
6906            self.pending_ops.iter().filter_map(|ops| {
6907                let inner_ops: Vec<PendingOp> =
6908                    ops.ops.iter().filter(|op| op.running()).cloned().collect();
6909                if inner_ops.is_empty() {
6910                    None
6911                } else {
6912                    Some(PendingOps {
6913                        repo_path: ops.repo_path.clone(),
6914                        ops: inner_ops,
6915                    })
6916                }
6917            }),
6918            (),
6919        );
6920
6921        if updated != self.pending_ops {
6922            cx.emit(RepositoryEvent::PendingOpsChanged {
6923                pending_ops: self.pending_ops.clone(),
6924            })
6925        }
6926
6927        self.pending_ops = updated;
6928    }
6929
6930    fn schedule_scan(
6931        &mut self,
6932        updates_tx: Option<mpsc::UnboundedSender<DownstreamUpdate>>,
6933        cx: &mut Context<Self>,
6934    ) {
6935        let this = cx.weak_entity();
6936        let _ = self.send_keyed_job(
6937            Some(GitJobKey::ReloadGitState),
6938            None,
6939            |state, mut cx| async move {
6940                log::debug!("run scheduled git status scan");
6941
6942                let Some(this) = this.upgrade() else {
6943                    return Ok(());
6944                };
6945                let RepositoryState::Local(LocalRepositoryState { backend, .. }) = state else {
6946                    bail!("not a local repository")
6947                };
6948                let snapshot = compute_snapshot(this.clone(), backend.clone(), &mut cx).await?;
6949                this.update(&mut cx, |this, cx| {
6950                    this.clear_pending_ops(cx);
6951                });
6952                if let Some(updates_tx) = updates_tx {
6953                    updates_tx
6954                        .unbounded_send(DownstreamUpdate::UpdateRepository(snapshot))
6955                        .ok();
6956                }
6957                Ok(())
6958            },
6959        );
6960    }
6961
6962    fn spawn_local_git_worker(
6963        state: Shared<Task<Result<LocalRepositoryState, String>>>,
6964        cx: &mut Context<Self>,
6965    ) -> mpsc::UnboundedSender<GitJob> {
6966        let (job_tx, mut job_rx) = mpsc::unbounded::<GitJob>();
6967
6968        cx.spawn(async move |_, cx| {
6969            let state = state.await.map_err(|err| anyhow::anyhow!(err))?;
6970            if let Some(git_hosting_provider_registry) =
6971                cx.update(|cx| GitHostingProviderRegistry::try_global(cx))
6972            {
6973                git_hosting_providers::register_additional_providers(
6974                    git_hosting_provider_registry,
6975                    state.backend.clone(),
6976                )
6977                .await;
6978            }
6979            let state = RepositoryState::Local(state);
6980            let mut jobs = VecDeque::new();
6981            loop {
6982                while let Ok(next_job) = job_rx.try_recv() {
6983                    jobs.push_back(next_job);
6984                }
6985
6986                if let Some(job) = jobs.pop_front() {
6987                    if let Some(current_key) = &job.key
6988                        && jobs
6989                            .iter()
6990                            .any(|other_job| other_job.key.as_ref() == Some(current_key))
6991                    {
6992                        continue;
6993                    }
6994                    (job.job)(state.clone(), cx).await;
6995                } else if let Some(job) = job_rx.next().await {
6996                    jobs.push_back(job);
6997                } else {
6998                    break;
6999                }
7000            }
7001            anyhow::Ok(())
7002        })
7003        .detach_and_log_err(cx);
7004
7005        job_tx
7006    }
7007
7008    fn spawn_remote_git_worker(
7009        state: RemoteRepositoryState,
7010        cx: &mut Context<Self>,
7011    ) -> mpsc::UnboundedSender<GitJob> {
7012        let (job_tx, mut job_rx) = mpsc::unbounded::<GitJob>();
7013
7014        cx.spawn(async move |_, cx| {
7015            let state = RepositoryState::Remote(state);
7016            let mut jobs = VecDeque::new();
7017            loop {
7018                while let Ok(next_job) = job_rx.try_recv() {
7019                    jobs.push_back(next_job);
7020                }
7021
7022                if let Some(job) = jobs.pop_front() {
7023                    if let Some(current_key) = &job.key
7024                        && jobs
7025                            .iter()
7026                            .any(|other_job| other_job.key.as_ref() == Some(current_key))
7027                    {
7028                        continue;
7029                    }
7030                    (job.job)(state.clone(), cx).await;
7031                } else if let Some(job) = job_rx.next().await {
7032                    jobs.push_back(job);
7033                } else {
7034                    break;
7035                }
7036            }
7037            anyhow::Ok(())
7038        })
7039        .detach_and_log_err(cx);
7040
7041        job_tx
7042    }
7043
7044    fn load_staged_text(
7045        &mut self,
7046        buffer_id: BufferId,
7047        repo_path: RepoPath,
7048        cx: &App,
7049    ) -> Task<Result<Option<String>>> {
7050        let rx = self.send_job(None, move |state, _| async move {
7051            match state {
7052                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
7053                    anyhow::Ok(backend.load_index_text(repo_path).await)
7054                }
7055                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
7056                    let response = client
7057                        .request(proto::OpenUnstagedDiff {
7058                            project_id: project_id.to_proto(),
7059                            buffer_id: buffer_id.to_proto(),
7060                        })
7061                        .await?;
7062                    Ok(response.staged_text)
7063                }
7064            }
7065        });
7066        cx.spawn(|_: &mut AsyncApp| async move { rx.await? })
7067    }
7068
7069    fn load_committed_text(
7070        &mut self,
7071        buffer_id: BufferId,
7072        repo_path: RepoPath,
7073        cx: &App,
7074    ) -> Task<Result<DiffBasesChange>> {
7075        let rx = self.send_job(None, move |state, _| async move {
7076            match state {
7077                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
7078                    let committed_text = backend.load_committed_text(repo_path.clone()).await;
7079                    let staged_text = backend.load_index_text(repo_path).await;
7080                    let diff_bases_change = if committed_text == staged_text {
7081                        DiffBasesChange::SetBoth(committed_text)
7082                    } else {
7083                        DiffBasesChange::SetEach {
7084                            index: staged_text,
7085                            head: committed_text,
7086                        }
7087                    };
7088                    anyhow::Ok(diff_bases_change)
7089                }
7090                RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
7091                    use proto::open_uncommitted_diff_response::Mode;
7092
7093                    let response = client
7094                        .request(proto::OpenUncommittedDiff {
7095                            project_id: project_id.to_proto(),
7096                            buffer_id: buffer_id.to_proto(),
7097                        })
7098                        .await?;
7099                    let mode = Mode::from_i32(response.mode).context("Invalid mode")?;
7100                    let bases = match mode {
7101                        Mode::IndexMatchesHead => DiffBasesChange::SetBoth(response.committed_text),
7102                        Mode::IndexAndHead => DiffBasesChange::SetEach {
7103                            head: response.committed_text,
7104                            index: response.staged_text,
7105                        },
7106                    };
7107                    Ok(bases)
7108                }
7109            }
7110        });
7111
7112        cx.spawn(|_: &mut AsyncApp| async move { rx.await? })
7113    }
7114
7115    pub fn load_commit_template_text(
7116        &mut self,
7117    ) -> oneshot::Receiver<Result<Option<GitCommitTemplate>>> {
7118        self.send_job(None, move |git_repo, _cx| async move {
7119            match git_repo {
7120                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
7121                    backend.load_commit_template().await
7122                }
7123                RepositoryState::Remote(_) => Ok(None),
7124            }
7125        })
7126    }
7127
7128    fn load_blob_content(&mut self, oid: Oid, cx: &App) -> Task<Result<String>> {
7129        let repository_id = self.snapshot.id;
7130        let rx = self.send_job(None, move |state, _| async move {
7131            match state {
7132                RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
7133                    backend.load_blob_content(oid).await
7134                }
7135                RepositoryState::Remote(RemoteRepositoryState { client, project_id }) => {
7136                    let response = client
7137                        .request(proto::GetBlobContent {
7138                            project_id: project_id.to_proto(),
7139                            repository_id: repository_id.0,
7140                            oid: oid.to_string(),
7141                        })
7142                        .await?;
7143                    Ok(response.content)
7144                }
7145            }
7146        });
7147        cx.spawn(|_: &mut AsyncApp| async move { rx.await? })
7148    }
7149
7150    fn paths_changed(
7151        &mut self,
7152        paths: Vec<RepoPath>,
7153        updates_tx: Option<mpsc::UnboundedSender<DownstreamUpdate>>,
7154        cx: &mut Context<Self>,
7155    ) {
7156        if !paths.is_empty() {
7157            self.paths_needing_status_update.push(paths);
7158        }
7159
7160        let this = cx.weak_entity();
7161        let _ = self.send_keyed_job(
7162            Some(GitJobKey::RefreshStatuses),
7163            None,
7164            |state, mut cx| async move {
7165                let (prev_snapshot, changed_paths) = this.update(&mut cx, |this, _| {
7166                    (
7167                        this.snapshot.clone(),
7168                        mem::take(&mut this.paths_needing_status_update),
7169                    )
7170                })?;
7171                let RepositoryState::Local(LocalRepositoryState { backend, .. }) = state else {
7172                    bail!("not a local repository")
7173                };
7174
7175                if changed_paths.is_empty() {
7176                    return Ok(());
7177                }
7178
7179                let has_head = prev_snapshot.head_commit.is_some();
7180
7181                let stash_entries = backend.stash_entries().await?;
7182                let changed_path_statuses = cx
7183                    .background_spawn(async move {
7184                        let mut changed_paths =
7185                            changed_paths.into_iter().flatten().collect::<BTreeSet<_>>();
7186                        let changed_paths_vec = changed_paths.iter().cloned().collect::<Vec<_>>();
7187
7188                        let status_task = backend.status(&changed_paths_vec);
7189                        let diff_stat_future = if has_head {
7190                            backend.diff_stat(&changed_paths_vec)
7191                        } else {
7192                            future::ready(Ok(status::GitDiffStat {
7193                                entries: Arc::default(),
7194                            }))
7195                            .boxed()
7196                        };
7197
7198                        let (statuses, diff_stats) =
7199                            futures::future::try_join(status_task, diff_stat_future).await?;
7200
7201                        let diff_stats: HashMap<RepoPath, DiffStat> =
7202                            HashMap::from_iter(diff_stats.entries.into_iter().cloned());
7203
7204                        let mut changed_path_statuses = Vec::new();
7205                        let prev_statuses = prev_snapshot.statuses_by_path.clone();
7206                        let mut cursor = prev_statuses.cursor::<PathProgress>(());
7207
7208                        for (repo_path, status) in &*statuses.entries {
7209                            let current_diff_stat = diff_stats.get(repo_path).copied();
7210
7211                            changed_paths.remove(repo_path);
7212                            if cursor.seek_forward(&PathTarget::Path(repo_path), Bias::Left)
7213                                && cursor.item().is_some_and(|entry| {
7214                                    entry.status == *status && entry.diff_stat == current_diff_stat
7215                                })
7216                            {
7217                                continue;
7218                            }
7219
7220                            changed_path_statuses.push(Edit::Insert(StatusEntry {
7221                                repo_path: repo_path.clone(),
7222                                status: *status,
7223                                diff_stat: current_diff_stat,
7224                            }));
7225                        }
7226                        let mut cursor = prev_statuses.cursor::<PathProgress>(());
7227                        for path in changed_paths.into_iter() {
7228                            if cursor.seek_forward(&PathTarget::Path(&path), Bias::Left) {
7229                                changed_path_statuses
7230                                    .push(Edit::Remove(PathKey(path.as_ref().clone())));
7231                            }
7232                        }
7233                        anyhow::Ok(changed_path_statuses)
7234                    })
7235                    .await?;
7236
7237                this.update(&mut cx, |this, cx| {
7238                    if this.snapshot.stash_entries != stash_entries {
7239                        cx.emit(RepositoryEvent::StashEntriesChanged);
7240                        this.snapshot.stash_entries = stash_entries;
7241                    }
7242
7243                    if !changed_path_statuses.is_empty() {
7244                        cx.emit(RepositoryEvent::StatusesChanged);
7245                        this.snapshot
7246                            .statuses_by_path
7247                            .edit(changed_path_statuses, ());
7248                        this.snapshot.scan_id += 1;
7249                    }
7250
7251                    if let Some(updates_tx) = updates_tx {
7252                        updates_tx
7253                            .unbounded_send(DownstreamUpdate::UpdateRepository(
7254                                this.snapshot.clone(),
7255                            ))
7256                            .ok();
7257                    }
7258                })
7259            },
7260        );
7261    }
7262
7263    /// currently running git command and when it started
7264    pub fn current_job(&self) -> Option<JobInfo> {
7265        self.active_jobs.values().next().cloned()
7266    }
7267
7268    pub fn barrier(&mut self) -> oneshot::Receiver<()> {
7269        self.send_job(None, |_, _| async {})
7270    }
7271
7272    fn spawn_job_with_tracking<AsyncFn>(
7273        &mut self,
7274        paths: Vec<RepoPath>,
7275        git_status: pending_op::GitStatus,
7276        cx: &mut Context<Self>,
7277        f: AsyncFn,
7278    ) -> Task<Result<()>>
7279    where
7280        AsyncFn: AsyncFnOnce(WeakEntity<Repository>, &mut AsyncApp) -> Result<()> + 'static,
7281    {
7282        let ids = self.new_pending_ops_for_paths(paths, git_status);
7283
7284        cx.spawn(async move |this, cx| {
7285            let (job_status, result) = match f(this.clone(), cx).await {
7286                Ok(()) => (pending_op::JobStatus::Finished, Ok(())),
7287                Err(err) if err.is::<Canceled>() => (pending_op::JobStatus::Skipped, Ok(())),
7288                Err(err) => (pending_op::JobStatus::Error, Err(err)),
7289            };
7290
7291            this.update(cx, |this, _| {
7292                let mut edits = Vec::with_capacity(ids.len());
7293                for (id, entry) in ids {
7294                    if let Some(mut ops) = this
7295                        .pending_ops
7296                        .get(&PathKey(entry.as_ref().clone()), ())
7297                        .cloned()
7298                    {
7299                        if let Some(op) = ops.op_by_id_mut(id) {
7300                            op.job_status = job_status;
7301                        }
7302                        edits.push(sum_tree::Edit::Insert(ops));
7303                    }
7304                }
7305                this.pending_ops.edit(edits, ());
7306            })?;
7307
7308            result
7309        })
7310    }
7311
7312    fn new_pending_ops_for_paths(
7313        &mut self,
7314        paths: Vec<RepoPath>,
7315        git_status: pending_op::GitStatus,
7316    ) -> Vec<(PendingOpId, RepoPath)> {
7317        let mut edits = Vec::with_capacity(paths.len());
7318        let mut ids = Vec::with_capacity(paths.len());
7319        for path in paths {
7320            let mut ops = self
7321                .pending_ops
7322                .get(&PathKey(path.as_ref().clone()), ())
7323                .cloned()
7324                .unwrap_or_else(|| PendingOps::new(&path));
7325            let id = ops.max_id() + 1;
7326            ops.ops.push(PendingOp {
7327                id,
7328                git_status,
7329                job_status: pending_op::JobStatus::Running,
7330            });
7331            edits.push(sum_tree::Edit::Insert(ops));
7332            ids.push((id, path));
7333        }
7334        self.pending_ops.edit(edits, ());
7335        ids
7336    }
7337    pub fn default_remote_url(&self) -> Option<String> {
7338        self.remote_upstream_url
7339            .clone()
7340            .or(self.remote_origin_url.clone())
7341    }
7342}
7343
7344/// If `path` is a git linked worktree checkout, resolves it to the main
7345/// repository's working directory path. Returns `None` if `path` is a normal
7346/// repository, not a git repo, or if resolution fails.
7347///
7348/// Resolution works by:
7349/// 1. Reading the `.git` file to get the `gitdir:` pointer
7350/// 2. Following that to the worktree-specific git directory
7351/// 3. Reading the `commondir` file to find the shared `.git` directory
7352/// 4. Deriving the main repo's working directory from the common dir
7353pub async fn resolve_git_worktree_to_main_repo(fs: &dyn Fs, path: &Path) -> Option<PathBuf> {
7354    let dot_git = path.join(".git");
7355    let metadata = fs.metadata(&dot_git).await.ok()??;
7356    if metadata.is_dir {
7357        return None; // Normal repo, not a linked worktree
7358    }
7359    // It's a .git file — parse the gitdir: pointer
7360    let content = fs.load(&dot_git).await.ok()?;
7361    let gitdir_rel = content.strip_prefix("gitdir:")?.trim();
7362    let gitdir_abs = fs.canonicalize(&path.join(gitdir_rel)).await.ok()?;
7363    // Read commondir to find the main .git directory
7364    let commondir_content = fs.load(&gitdir_abs.join("commondir")).await.ok()?;
7365    let common_dir = fs
7366        .canonicalize(&gitdir_abs.join(commondir_content.trim()))
7367        .await
7368        .ok()?;
7369    git::repository::original_repo_path_from_common_dir(&common_dir)
7370}
7371
7372/// Validates that the resolved worktree directory is acceptable:
7373/// - The setting must not be an absolute path.
7374/// - The resolved path must be either a subdirectory of the working
7375///   directory or a subdirectory of its parent (i.e., a sibling).
7376///
7377/// Returns `Ok(resolved_path)` or an error with a user-facing message.
7378pub fn worktrees_directory_for_repo(
7379    original_repo_abs_path: &Path,
7380    worktree_directory_setting: &str,
7381) -> Result<PathBuf> {
7382    // Check the original setting before trimming, since a path like "///"
7383    // is absolute but becomes "" after stripping trailing separators.
7384    // Also check for leading `/` or `\` explicitly, because on Windows
7385    // `Path::is_absolute()` requires a drive letter — so `/tmp/worktrees`
7386    // would slip through even though it's clearly not a relative path.
7387    if Path::new(worktree_directory_setting).is_absolute()
7388        || worktree_directory_setting.starts_with('/')
7389        || worktree_directory_setting.starts_with('\\')
7390    {
7391        anyhow::bail!(
7392            "git.worktree_directory must be a relative path, got: {worktree_directory_setting:?}"
7393        );
7394    }
7395
7396    if worktree_directory_setting.is_empty() {
7397        anyhow::bail!("git.worktree_directory must not be empty");
7398    }
7399
7400    let trimmed = worktree_directory_setting.trim_end_matches(['/', '\\']);
7401    if trimmed == ".." {
7402        anyhow::bail!("git.worktree_directory must not be \"..\" (use \"../some-name\" instead)");
7403    }
7404
7405    let joined = original_repo_abs_path.join(trimmed);
7406    let resolved = util::normalize_path(&joined);
7407    let resolved = if resolved.starts_with(original_repo_abs_path) {
7408        resolved
7409    } else if let Some(repo_dir_name) = original_repo_abs_path.file_name() {
7410        resolved.join(repo_dir_name)
7411    } else {
7412        resolved
7413    };
7414
7415    let parent = original_repo_abs_path
7416        .parent()
7417        .unwrap_or(original_repo_abs_path);
7418
7419    if !resolved.starts_with(parent) {
7420        anyhow::bail!(
7421            "git.worktree_directory resolved to {resolved:?}, which is outside \
7422             the project root and its parent directory. It must resolve to a \
7423             subdirectory of {original_repo_abs_path:?} or a sibling of it."
7424        );
7425    }
7426
7427    Ok(resolved)
7428}
7429
7430async fn remove_empty_managed_worktree_ancestors(fs: &dyn Fs, child_path: &Path, base_path: &Path) {
7431    let mut current = child_path;
7432    while let Some(parent) = current.parent() {
7433        if parent == base_path {
7434            break;
7435        }
7436        if !parent.starts_with(base_path) {
7437            break;
7438        }
7439
7440        let result = fs
7441            .remove_dir(
7442                parent,
7443                RemoveOptions {
7444                    recursive: false,
7445                    ignore_if_not_exists: true,
7446                },
7447            )
7448            .await;
7449
7450        match result {
7451            Ok(()) => {
7452                log::info!(
7453                    "Removed empty managed worktree directory: {}",
7454                    parent.display()
7455                );
7456            }
7457            Err(error) => {
7458                log::debug!(
7459                    "Stopped removing managed worktree parent directories at {}: {error}",
7460                    parent.display()
7461                );
7462                break;
7463            }
7464        }
7465
7466        current = parent;
7467    }
7468}
7469
7470/// Returns a short name for a linked worktree suitable for UI display
7471///
7472/// Uses the main worktree path to come up with a short name that disambiguates
7473/// the linked worktree from the main worktree.
7474pub fn linked_worktree_short_name(
7475    main_worktree_path: &Path,
7476    linked_worktree_path: &Path,
7477) -> Option<SharedString> {
7478    if main_worktree_path == linked_worktree_path {
7479        return None;
7480    }
7481
7482    let project_name = main_worktree_path.file_name()?.to_str()?;
7483    let directory_name = linked_worktree_path.file_name()?.to_str()?;
7484    let name = if directory_name != project_name {
7485        directory_name.to_string()
7486    } else {
7487        linked_worktree_path
7488            .parent()?
7489            .file_name()?
7490            .to_str()?
7491            .to_string()
7492    };
7493    Some(name.into())
7494}
7495
7496fn get_permalink_in_rust_registry_src(
7497    provider_registry: Arc<GitHostingProviderRegistry>,
7498    path: PathBuf,
7499    selection: Range<u32>,
7500) -> Result<url::Url> {
7501    #[derive(Deserialize)]
7502    struct CargoVcsGit {
7503        sha1: String,
7504    }
7505
7506    #[derive(Deserialize)]
7507    struct CargoVcsInfo {
7508        git: CargoVcsGit,
7509        path_in_vcs: String,
7510    }
7511
7512    #[derive(Deserialize)]
7513    struct CargoPackage {
7514        repository: String,
7515    }
7516
7517    #[derive(Deserialize)]
7518    struct CargoToml {
7519        package: CargoPackage,
7520    }
7521
7522    let Some((dir, cargo_vcs_info_json)) = path.ancestors().skip(1).find_map(|dir| {
7523        let json = std::fs::read_to_string(dir.join(".cargo_vcs_info.json")).ok()?;
7524        Some((dir, json))
7525    }) else {
7526        bail!("No .cargo_vcs_info.json found in parent directories")
7527    };
7528    let cargo_vcs_info = serde_json::from_str::<CargoVcsInfo>(&cargo_vcs_info_json)?;
7529    let cargo_toml = std::fs::read_to_string(dir.join("Cargo.toml"))?;
7530    let manifest = toml::from_str::<CargoToml>(&cargo_toml)?;
7531    let (provider, remote) = parse_git_remote_url(provider_registry, &manifest.package.repository)
7532        .context("parsing package.repository field of manifest")?;
7533    let path = PathBuf::from(cargo_vcs_info.path_in_vcs).join(path.strip_prefix(dir).unwrap());
7534    let permalink = provider.build_permalink(
7535        remote,
7536        BuildPermalinkParams::new(
7537            &cargo_vcs_info.git.sha1,
7538            &RepoPath::from_rel_path(
7539                &RelPath::new(&path, PathStyle::local()).context("invalid path")?,
7540            ),
7541            Some(selection),
7542        ),
7543    );
7544    Ok(permalink)
7545}
7546
7547fn serialize_blame_buffer_response(blame: Option<git::blame::Blame>) -> proto::BlameBufferResponse {
7548    let Some(blame) = blame else {
7549        return proto::BlameBufferResponse {
7550            blame_response: None,
7551        };
7552    };
7553
7554    let entries = blame
7555        .entries
7556        .into_iter()
7557        .map(|entry| proto::BlameEntry {
7558            sha: entry.sha.as_bytes().into(),
7559            start_line: entry.range.start,
7560            end_line: entry.range.end,
7561            original_line_number: entry.original_line_number,
7562            author: entry.author,
7563            author_mail: entry.author_mail,
7564            author_time: entry.author_time,
7565            author_tz: entry.author_tz,
7566            committer: entry.committer_name,
7567            committer_mail: entry.committer_email,
7568            committer_time: entry.committer_time,
7569            committer_tz: entry.committer_tz,
7570            summary: entry.summary,
7571            previous: entry.previous,
7572            filename: entry.filename,
7573        })
7574        .collect::<Vec<_>>();
7575
7576    let messages = blame
7577        .messages
7578        .into_iter()
7579        .map(|(oid, message)| proto::CommitMessage {
7580            oid: oid.as_bytes().into(),
7581            message,
7582        })
7583        .collect::<Vec<_>>();
7584
7585    proto::BlameBufferResponse {
7586        blame_response: Some(proto::blame_buffer_response::BlameResponse { entries, messages }),
7587    }
7588}
7589
7590fn deserialize_blame_buffer_response(
7591    response: proto::BlameBufferResponse,
7592) -> Option<git::blame::Blame> {
7593    let response = response.blame_response?;
7594    let entries = response
7595        .entries
7596        .into_iter()
7597        .filter_map(|entry| {
7598            Some(git::blame::BlameEntry {
7599                sha: git::Oid::from_bytes(&entry.sha).ok()?,
7600                range: entry.start_line..entry.end_line,
7601                original_line_number: entry.original_line_number,
7602                committer_name: entry.committer,
7603                committer_time: entry.committer_time,
7604                committer_tz: entry.committer_tz,
7605                committer_email: entry.committer_mail,
7606                author: entry.author,
7607                author_mail: entry.author_mail,
7608                author_time: entry.author_time,
7609                author_tz: entry.author_tz,
7610                summary: entry.summary,
7611                previous: entry.previous,
7612                filename: entry.filename,
7613            })
7614        })
7615        .collect::<Vec<_>>();
7616
7617    let messages = response
7618        .messages
7619        .into_iter()
7620        .filter_map(|message| Some((git::Oid::from_bytes(&message.oid).ok()?, message.message)))
7621        .collect::<HashMap<_, _>>();
7622
7623    Some(Blame { entries, messages })
7624}
7625
7626fn branch_to_proto(branch: &git::repository::Branch) -> proto::Branch {
7627    proto::Branch {
7628        is_head: branch.is_head,
7629        ref_name: branch.ref_name.to_string(),
7630        unix_timestamp: branch
7631            .most_recent_commit
7632            .as_ref()
7633            .map(|commit| commit.commit_timestamp as u64),
7634        upstream: branch.upstream.as_ref().map(|upstream| proto::GitUpstream {
7635            ref_name: upstream.ref_name.to_string(),
7636            tracking: upstream
7637                .tracking
7638                .status()
7639                .map(|upstream| proto::UpstreamTracking {
7640                    ahead: upstream.ahead as u64,
7641                    behind: upstream.behind as u64,
7642                }),
7643        }),
7644        most_recent_commit: branch
7645            .most_recent_commit
7646            .as_ref()
7647            .map(|commit| proto::CommitSummary {
7648                sha: commit.sha.to_string(),
7649                subject: commit.subject.to_string(),
7650                commit_timestamp: commit.commit_timestamp,
7651                author_name: commit.author_name.to_string(),
7652            }),
7653    }
7654}
7655
7656fn worktree_to_proto(worktree: &git::repository::Worktree) -> proto::Worktree {
7657    proto::Worktree {
7658        path: worktree.path.to_string_lossy().to_string(),
7659        ref_name: worktree
7660            .ref_name
7661            .as_ref()
7662            .map(|s| s.to_string())
7663            .unwrap_or_default(),
7664        sha: worktree.sha.to_string(),
7665        is_main: worktree.is_main,
7666        is_bare: worktree.is_bare,
7667    }
7668}
7669
7670fn proto_to_worktree(proto: &proto::Worktree) -> git::repository::Worktree {
7671    git::repository::Worktree {
7672        path: PathBuf::from(proto.path.clone()),
7673        ref_name: if proto.ref_name.is_empty() {
7674            None
7675        } else {
7676            Some(SharedString::from(&proto.ref_name))
7677        },
7678        sha: proto.sha.clone().into(),
7679        is_main: proto.is_main,
7680        is_bare: proto.is_bare,
7681    }
7682}
7683
7684fn proto_to_branch(proto: &proto::Branch) -> git::repository::Branch {
7685    git::repository::Branch {
7686        is_head: proto.is_head,
7687        ref_name: proto.ref_name.clone().into(),
7688        upstream: proto
7689            .upstream
7690            .as_ref()
7691            .map(|upstream| git::repository::Upstream {
7692                ref_name: upstream.ref_name.to_string().into(),
7693                tracking: upstream
7694                    .tracking
7695                    .as_ref()
7696                    .map(|tracking| {
7697                        git::repository::UpstreamTracking::Tracked(UpstreamTrackingStatus {
7698                            ahead: tracking.ahead as u32,
7699                            behind: tracking.behind as u32,
7700                        })
7701                    })
7702                    .unwrap_or(git::repository::UpstreamTracking::Gone),
7703            }),
7704        most_recent_commit: proto.most_recent_commit.as_ref().map(|commit| {
7705            git::repository::CommitSummary {
7706                sha: commit.sha.to_string().into(),
7707                subject: commit.subject.to_string().into(),
7708                commit_timestamp: commit.commit_timestamp,
7709                author_name: commit.author_name.to_string().into(),
7710                has_parent: true,
7711            }
7712        }),
7713    }
7714}
7715
7716fn commit_details_to_proto(commit: &CommitDetails) -> proto::GitCommitDetails {
7717    proto::GitCommitDetails {
7718        sha: commit.sha.to_string(),
7719        message: commit.message.to_string(),
7720        commit_timestamp: commit.commit_timestamp,
7721        author_email: commit.author_email.to_string(),
7722        author_name: commit.author_name.to_string(),
7723    }
7724}
7725
7726fn proto_to_commit_details(proto: &proto::GitCommitDetails) -> CommitDetails {
7727    CommitDetails {
7728        sha: proto.sha.clone().into(),
7729        message: proto.message.clone().into(),
7730        commit_timestamp: proto.commit_timestamp,
7731        author_email: proto.author_email.clone().into(),
7732        author_name: proto.author_name.clone().into(),
7733    }
7734}
7735
7736/// This snapshot computes the repository state on the foreground thread while
7737/// running the git commands on the background thread. We update branch, head,
7738/// remotes, and worktrees first so the UI can react sooner, then compute file
7739/// state and emit those events immediately after.
7740async fn compute_snapshot(
7741    this: Entity<Repository>,
7742    backend: Arc<dyn GitRepository>,
7743    cx: &mut AsyncApp,
7744) -> Result<RepositorySnapshot> {
7745    let (id, work_directory_abs_path, prev_snapshot) = this.update(cx, |this, _| {
7746        this.paths_needing_status_update.clear();
7747        (
7748            this.id,
7749            this.work_directory_abs_path.clone(),
7750            this.snapshot.clone(),
7751        )
7752    });
7753
7754    let head_commit_future = {
7755        let backend = backend.clone();
7756        async move {
7757            Ok(match backend.head_sha().await {
7758                Some(head_sha) => backend.show(head_sha).await.log_err(),
7759                None => None,
7760            })
7761        }
7762    };
7763    let (branches, head_commit, all_worktrees) = cx
7764        .background_spawn({
7765            let backend = backend.clone();
7766            async move {
7767                futures::future::try_join3(
7768                    backend.branches(),
7769                    head_commit_future,
7770                    backend.worktrees(),
7771                )
7772                .await
7773            }
7774        })
7775        .await?;
7776    let branch = branches.iter().find(|branch| branch.is_head).cloned();
7777    let branch_list: Arc<[Branch]> = branches.into();
7778
7779    let linked_worktrees: Arc<[GitWorktree]> = all_worktrees
7780        .into_iter()
7781        .filter(|wt| wt.path != *work_directory_abs_path)
7782        .collect();
7783
7784    let (remote_origin_url, remote_upstream_url) = cx
7785        .background_spawn({
7786            let backend = backend.clone();
7787            async move {
7788                Ok::<_, anyhow::Error>(
7789                    futures::future::join(
7790                        backend.remote_url("origin"),
7791                        backend.remote_url("upstream"),
7792                    )
7793                    .await,
7794                )
7795            }
7796        })
7797        .await?;
7798
7799    let snapshot = this.update(cx, |this, cx| {
7800        let head_changed =
7801            branch != this.snapshot.branch || head_commit != this.snapshot.head_commit;
7802        let branch_list_changed = *branch_list != *this.snapshot.branch_list;
7803        let worktrees_changed = *linked_worktrees != *this.snapshot.linked_worktrees;
7804
7805        this.snapshot = RepositorySnapshot {
7806            id,
7807            work_directory_abs_path,
7808            branch,
7809            branch_list: branch_list.clone(),
7810            head_commit,
7811            remote_origin_url,
7812            remote_upstream_url,
7813            linked_worktrees,
7814            scan_id: prev_snapshot.scan_id + 1,
7815            ..prev_snapshot
7816        };
7817
7818        if head_changed {
7819            cx.emit(RepositoryEvent::HeadChanged);
7820        }
7821
7822        if branch_list_changed {
7823            cx.emit(RepositoryEvent::BranchListChanged);
7824        }
7825
7826        if worktrees_changed {
7827            cx.emit(RepositoryEvent::GitWorktreeListChanged);
7828        }
7829
7830        this.snapshot.clone()
7831    });
7832
7833    let (statuses, diff_stats, stash_entries) = cx
7834        .background_spawn({
7835            let backend = backend.clone();
7836            let snapshot = snapshot.clone();
7837            async move {
7838                let diff_stat_future: BoxFuture<'_, Result<status::GitDiffStat>> =
7839                    if snapshot.head_commit.is_some() {
7840                        backend.diff_stat(&[])
7841                    } else {
7842                        future::ready(Ok(status::GitDiffStat {
7843                            entries: Arc::default(),
7844                        }))
7845                        .boxed()
7846                    };
7847                futures::future::try_join3(
7848                    backend.status(&[RepoPath::from_rel_path(
7849                        &RelPath::new(".".as_ref(), PathStyle::local()).unwrap(),
7850                    )]),
7851                    diff_stat_future,
7852                    backend.stash_entries(),
7853                )
7854                .await
7855            }
7856        })
7857        .await?;
7858
7859    let diff_stat_map: HashMap<&RepoPath, DiffStat> =
7860        diff_stats.entries.iter().map(|(p, s)| (p, *s)).collect();
7861    let mut conflicted_paths = Vec::new();
7862    let statuses_by_path = SumTree::from_iter(
7863        statuses.entries.iter().map(|(repo_path, status)| {
7864            if status.is_conflicted() {
7865                conflicted_paths.push(repo_path.clone());
7866            }
7867            StatusEntry {
7868                repo_path: repo_path.clone(),
7869                status: *status,
7870                diff_stat: diff_stat_map.get(repo_path).copied(),
7871            }
7872        }),
7873        (),
7874    );
7875
7876    let merge_details = cx
7877        .background_spawn({
7878            let backend = backend.clone();
7879            let mut merge_details = snapshot.merge.clone();
7880            async move {
7881                let conflicts_changed = merge_details.update(&backend, conflicted_paths).await?;
7882                Ok::<_, anyhow::Error>((merge_details, conflicts_changed))
7883            }
7884        })
7885        .await?;
7886    let (merge_details, conflicts_changed) = merge_details;
7887    log::debug!("new merge details: {merge_details:?}");
7888
7889    Ok(this.update(cx, |this, cx| {
7890        if conflicts_changed || statuses_by_path != this.snapshot.statuses_by_path {
7891            cx.emit(RepositoryEvent::StatusesChanged);
7892        }
7893        if stash_entries != this.snapshot.stash_entries {
7894            cx.emit(RepositoryEvent::StashEntriesChanged);
7895        }
7896
7897        this.snapshot.scan_id += 1;
7898        this.snapshot.merge = merge_details;
7899        this.snapshot.statuses_by_path = statuses_by_path;
7900        this.snapshot.stash_entries = stash_entries;
7901
7902        this.snapshot.clone()
7903    }))
7904}
7905
7906fn status_from_proto(
7907    simple_status: i32,
7908    status: Option<proto::GitFileStatus>,
7909) -> anyhow::Result<FileStatus> {
7910    use proto::git_file_status::Variant;
7911
7912    let Some(variant) = status.and_then(|status| status.variant) else {
7913        let code = proto::GitStatus::from_i32(simple_status)
7914            .with_context(|| format!("Invalid git status code: {simple_status}"))?;
7915        let result = match code {
7916            proto::GitStatus::Added => TrackedStatus {
7917                worktree_status: StatusCode::Added,
7918                index_status: StatusCode::Unmodified,
7919            }
7920            .into(),
7921            proto::GitStatus::Modified => TrackedStatus {
7922                worktree_status: StatusCode::Modified,
7923                index_status: StatusCode::Unmodified,
7924            }
7925            .into(),
7926            proto::GitStatus::Conflict => UnmergedStatus {
7927                first_head: UnmergedStatusCode::Updated,
7928                second_head: UnmergedStatusCode::Updated,
7929            }
7930            .into(),
7931            proto::GitStatus::Deleted => TrackedStatus {
7932                worktree_status: StatusCode::Deleted,
7933                index_status: StatusCode::Unmodified,
7934            }
7935            .into(),
7936            _ => anyhow::bail!("Invalid code for simple status: {simple_status}"),
7937        };
7938        return Ok(result);
7939    };
7940
7941    let result = match variant {
7942        Variant::Untracked(_) => FileStatus::Untracked,
7943        Variant::Ignored(_) => FileStatus::Ignored,
7944        Variant::Unmerged(unmerged) => {
7945            let [first_head, second_head] =
7946                [unmerged.first_head, unmerged.second_head].map(|head| {
7947                    let code = proto::GitStatus::from_i32(head)
7948                        .with_context(|| format!("Invalid git status code: {head}"))?;
7949                    let result = match code {
7950                        proto::GitStatus::Added => UnmergedStatusCode::Added,
7951                        proto::GitStatus::Updated => UnmergedStatusCode::Updated,
7952                        proto::GitStatus::Deleted => UnmergedStatusCode::Deleted,
7953                        _ => anyhow::bail!("Invalid code for unmerged status: {code:?}"),
7954                    };
7955                    Ok(result)
7956                });
7957            let [first_head, second_head] = [first_head?, second_head?];
7958            UnmergedStatus {
7959                first_head,
7960                second_head,
7961            }
7962            .into()
7963        }
7964        Variant::Tracked(tracked) => {
7965            let [index_status, worktree_status] = [tracked.index_status, tracked.worktree_status]
7966                .map(|status| {
7967                    let code = proto::GitStatus::from_i32(status)
7968                        .with_context(|| format!("Invalid git status code: {status}"))?;
7969                    let result = match code {
7970                        proto::GitStatus::Modified => StatusCode::Modified,
7971                        proto::GitStatus::TypeChanged => StatusCode::TypeChanged,
7972                        proto::GitStatus::Added => StatusCode::Added,
7973                        proto::GitStatus::Deleted => StatusCode::Deleted,
7974                        proto::GitStatus::Renamed => StatusCode::Renamed,
7975                        proto::GitStatus::Copied => StatusCode::Copied,
7976                        proto::GitStatus::Unmodified => StatusCode::Unmodified,
7977                        _ => anyhow::bail!("Invalid code for tracked status: {code:?}"),
7978                    };
7979                    Ok(result)
7980                });
7981            let [index_status, worktree_status] = [index_status?, worktree_status?];
7982            TrackedStatus {
7983                index_status,
7984                worktree_status,
7985            }
7986            .into()
7987        }
7988    };
7989    Ok(result)
7990}
7991
7992fn status_to_proto(status: FileStatus) -> proto::GitFileStatus {
7993    use proto::git_file_status::{Tracked, Unmerged, Variant};
7994
7995    let variant = match status {
7996        FileStatus::Untracked => Variant::Untracked(Default::default()),
7997        FileStatus::Ignored => Variant::Ignored(Default::default()),
7998        FileStatus::Unmerged(UnmergedStatus {
7999            first_head,
8000            second_head,
8001        }) => Variant::Unmerged(Unmerged {
8002            first_head: unmerged_status_to_proto(first_head),
8003            second_head: unmerged_status_to_proto(second_head),
8004        }),
8005        FileStatus::Tracked(TrackedStatus {
8006            index_status,
8007            worktree_status,
8008        }) => Variant::Tracked(Tracked {
8009            index_status: tracked_status_to_proto(index_status),
8010            worktree_status: tracked_status_to_proto(worktree_status),
8011        }),
8012    };
8013    proto::GitFileStatus {
8014        variant: Some(variant),
8015    }
8016}
8017
8018fn unmerged_status_to_proto(code: UnmergedStatusCode) -> i32 {
8019    match code {
8020        UnmergedStatusCode::Added => proto::GitStatus::Added as _,
8021        UnmergedStatusCode::Deleted => proto::GitStatus::Deleted as _,
8022        UnmergedStatusCode::Updated => proto::GitStatus::Updated as _,
8023    }
8024}
8025
8026fn tracked_status_to_proto(code: StatusCode) -> i32 {
8027    match code {
8028        StatusCode::Added => proto::GitStatus::Added as _,
8029        StatusCode::Deleted => proto::GitStatus::Deleted as _,
8030        StatusCode::Modified => proto::GitStatus::Modified as _,
8031        StatusCode::Renamed => proto::GitStatus::Renamed as _,
8032        StatusCode::TypeChanged => proto::GitStatus::TypeChanged as _,
8033        StatusCode::Copied => proto::GitStatus::Copied as _,
8034        StatusCode::Unmodified => proto::GitStatus::Unmodified as _,
8035    }
8036}