Move git state to Project (#23208)

This restores its visibility outside of git_ui, which we'll need soon,
while preserving its per-project character.

Release Notes:

- N/A
This commit is contained in:
Cole Miller 2025-01-16 13:57:28 -05:00 committed by GitHub
parent 614eaec278
commit b7726238ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 363 additions and 317 deletions

124
crates/project/src/git.rs Normal file
View file

@ -0,0 +1,124 @@
use std::sync::Arc;
use futures::channel::mpsc;
use futures::StreamExt as _;
use git::repository::{GitRepository, RepoPath};
use gpui::{AppContext, SharedString};
use settings::WorktreeId;
use util::ResultExt as _;
use worktree::RepositoryEntry;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StatusAction {
Stage,
Unstage,
}
pub struct GitState {
/// The current commit message being composed.
pub commit_message: Option<SharedString>,
/// When a git repository is selected, this is used to track which repository's changes
/// are currently being viewed or modified in the UI.
pub active_repository: Option<(WorktreeId, RepositoryEntry, Arc<dyn GitRepository>)>,
pub update_sender: mpsc::UnboundedSender<(Arc<dyn GitRepository>, Vec<RepoPath>, StatusAction)>,
}
impl GitState {
pub fn new(cx: &AppContext) -> Self {
let (tx, mut rx) =
mpsc::unbounded::<(Arc<dyn GitRepository>, Vec<RepoPath>, StatusAction)>();
cx.spawn(|cx| async move {
while let Some((git_repo, paths, action)) = rx.next().await {
cx.background_executor()
.spawn(async move {
match action {
StatusAction::Stage => git_repo.stage_paths(&paths),
StatusAction::Unstage => git_repo.unstage_paths(&paths),
}
})
.await
.log_err();
}
})
.detach();
GitState {
commit_message: None,
active_repository: None,
update_sender: tx,
}
}
pub fn activate_repository(
&mut self,
worktree_id: WorktreeId,
active_repository: RepositoryEntry,
git_repo: Arc<dyn GitRepository>,
) {
self.active_repository = Some((worktree_id, active_repository, git_repo));
}
pub fn active_repository(
&self,
) -> Option<&(WorktreeId, RepositoryEntry, Arc<dyn GitRepository>)> {
self.active_repository.as_ref()
}
pub fn commit_message(&mut self, message: Option<SharedString>) {
self.commit_message = message;
}
pub fn clear_commit_message(&mut self) {
self.commit_message = None;
}
pub fn stage_entry(&mut self, repo_path: RepoPath) {
if let Some((_, _, git_repo)) = self.active_repository.as_ref() {
let _ = self.update_sender.unbounded_send((
git_repo.clone(),
vec![repo_path],
StatusAction::Stage,
));
}
}
pub fn unstage_entry(&mut self, repo_path: RepoPath) {
if let Some((_, _, git_repo)) = self.active_repository.as_ref() {
let _ = self.update_sender.unbounded_send((
git_repo.clone(),
vec![repo_path],
StatusAction::Unstage,
));
}
}
pub fn stage_entries(&mut self, entries: Vec<RepoPath>) {
if let Some((_, _, git_repo)) = self.active_repository.as_ref() {
let _ =
self.update_sender
.unbounded_send((git_repo.clone(), entries, StatusAction::Stage));
}
}
fn act_on_all(&mut self, action: StatusAction) {
if let Some((_, active_repository, git_repo)) = self.active_repository.as_ref() {
let _ = self.update_sender.unbounded_send((
git_repo.clone(),
active_repository
.status()
.map(|entry| entry.repo_path)
.collect(),
action,
));
}
}
pub fn stage_all(&mut self) {
self.act_on_all(StatusAction::Stage);
}
pub fn unstage_all(&mut self) {
self.act_on_all(StatusAction::Unstage);
}
}

View file

@ -2,6 +2,7 @@ pub mod buffer_store;
mod color_extractor;
pub mod connection_manager;
pub mod debounced_delay;
pub mod git;
pub mod image_store;
pub mod lsp_command;
pub mod lsp_ext_command;
@ -24,6 +25,7 @@ pub use environment::EnvironmentErrorMessage;
pub mod search_history;
mod yarn;
use crate::git::GitState;
use anyhow::{anyhow, Context as _, Result};
use buffer_store::{BufferChangeSet, BufferStore, BufferStoreEvent};
use client::{proto, Client, Collaborator, PendingEntitySubscription, TypedEnvelope, UserStore};
@ -39,7 +41,11 @@ use futures::{
pub use image_store::{ImageItem, ImageStore};
use image_store::{ImageItemEvent, ImageStoreEvent};
use git::{blame::Blame, repository::GitRepository, status::FileStatus};
use ::git::{
blame::Blame,
repository::{Branch, GitRepository},
status::FileStatus,
};
use gpui::{
AnyModel, AppContext, AsyncAppContext, BorrowAppContext, Context as _, EventEmitter, Hsla,
Model, ModelContext, SharedString, Task, WeakModel, WindowContext,
@ -148,6 +154,7 @@ pub struct Project {
fs: Arc<dyn Fs>,
ssh_client: Option<Model<SshRemoteClient>>,
client_state: ProjectClientState,
git_state: Option<Model<GitState>>,
collaborators: HashMap<proto::PeerId, Collaborator>,
client_subscriptions: Vec<client::Subscription>,
worktree_store: Model<WorktreeStore>,
@ -685,6 +692,9 @@ impl Project {
cx,
)
});
let git_state = Some(cx.new_model(|cx| GitState::new(cx)));
cx.subscribe(&lsp_store, Self::on_lsp_store_event).detach();
Self {
@ -696,6 +706,7 @@ impl Project {
lsp_store,
join_project_response_message_id: 0,
client_state: ProjectClientState::Local,
git_state,
client_subscriptions: Vec::new(),
_subscriptions: vec![cx.on_release(Self::release)],
active_entry: None,
@ -814,6 +825,7 @@ impl Project {
lsp_store,
join_project_response_message_id: 0,
client_state: ProjectClientState::Local,
git_state: None,
client_subscriptions: Vec::new(),
_subscriptions: vec![
cx.on_release(Self::release),
@ -1045,6 +1057,7 @@ impl Project {
remote_id,
replica_id,
},
git_state: None,
buffers_needing_diff: Default::default(),
git_diff_debouncer: DebouncedDelay::new(),
terminals: Terminals {
@ -3534,7 +3547,7 @@ impl Project {
&self,
project_path: ProjectPath,
cx: &AppContext,
) -> Task<Result<Vec<git::repository::Branch>>> {
) -> Task<Result<Vec<Branch>>> {
self.worktree_store().read(cx).branches(project_path, cx)
}
@ -4154,6 +4167,10 @@ impl Project {
pub fn buffer_store(&self) -> &Model<BufferStore> {
&self.buffer_store
}
pub fn git_state(&self) -> Option<&Model<GitState>> {
self.git_state.as_ref()
}
}
fn deserialize_code_actions(code_actions: &HashMap<String, bool>) -> Vec<lsp::CodeActionKind> {

View file

@ -1,7 +1,7 @@
use crate::{Event, *};
use ::git::diff::assert_hunks;
use fs::FakeFs;
use futures::{future, StreamExt};
use git::diff::assert_hunks;
use gpui::{AppContext, SemanticVersion, UpdateGlobal};
use http_client::Url;
use language::{