Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
git_graph: Add remote support (#55788)
Anthony Eid4e082632fcbc
diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rsindex 20facc32640..d5c6565b6cc 100644--- a/crates/project/src/git_store.rs+++ b/crates/project/src/git_store.rs@@ -361,6 +361,7 @@ pub struct InitialGitGraphData {pub error: Option<SharedString>,pub commit_data: Vec<Arc<InitialGraphCommitData>>,pub commit_oid_to_index: HashMap<Oid, usize>,+ subscribers: Vec<async_channel::Sender<Result<Vec<Arc<InitialGraphCommitData>>, SharedString>>>,}pub struct GraphDataResponse<'a> {@@ -680,6 +681,7 @@ impl GitStore {client.add_entity_request_handler(Self::handle_edit_ref);client.add_entity_request_handler(Self::handle_repair_worktrees);client.add_entity_request_handler(Self::handle_get_commit_data);+ client.add_entity_stream_request_handler(Self::handle_get_initial_graph_data);client.add_entity_stream_request_handler(Self::handle_search_commits);}@@ -2670,6 +2672,105 @@ impl GitStore {Ok(proto::GetCommitDataResponse { commits })}+ async fn handle_get_initial_graph_data(+ this: Entity<Self>,+ envelope: TypedEnvelope<proto::GetInitialGraphData>,+ mut cx: AsyncApp,+ ) -> Result<impl Stream<Item = Result<proto::GetInitialGraphDataResponse>>> {+ const CHUNK_SIZE: usize = git::repository::GRAPH_CHUNK_SIZE;+ let payload = envelope.payload;++ let repository_id = RepositoryId::from_proto(payload.repository_id);+ let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;++ let log_order = log_order_from_proto(payload.log_order());+ let log_source = log_source_from_proto(+ payload+ .log_source+ .context("missing initial graph data log source")?,+ )?;++ let (subscriber_sender, subscriber_receiver) = async_channel::unbounded();+ let (cached_commits, error, is_loading) =+ repository_handle.update(&mut cx, |repository, cx| {+ let response =+ repository.graph_data(log_source.clone(), log_order, 0..usize::MAX, cx);+ let cached_commits = response.commits.to_vec();+ let error = response.error.clone();+ let is_loading = response.is_loading;++ if is_loading {+ if let Some(graph_data) = repository+ .initial_graph_data+ .get_mut(&(log_source.clone(), log_order))+ {+ graph_data.subscribers.push(subscriber_sender);+ }+ }++ (cached_commits, error, is_loading)+ });++ let (mut response_tx, response_rx) = mpsc::unbounded();+ cx.background_spawn(async move {+ if let Some(error) = error {+ if response_tx+ .send(Err(anyhow!(error.to_string())))+ .await+ .is_err()+ {+ return;+ }+ return;+ }++ for commits in cached_commits.chunks(CHUNK_SIZE) {+ let response = proto::GetInitialGraphDataResponse {+ commits: commits+ .iter()+ .map(|commit| initial_graph_commit_to_proto(commit))+ .collect(),+ };+ if response_tx.send(Ok(response)).await.is_err() {+ return;+ }+ }++ if !is_loading {+ return;+ }++ while let Ok(chunk_result) = subscriber_receiver.recv().await {+ let commits = match chunk_result {+ Ok(commits) => commits,+ Err(error) => {+ response_tx+ .send(Err(anyhow!(error.to_string())))+ .await+ .context("Failed to send error")+ .log_err();+ return;+ }+ };++ for commits in commits.chunks(CHUNK_SIZE) {+ let response = proto::GetInitialGraphDataResponse {+ commits: commits+ .iter()+ .map(|commit| initial_graph_commit_to_proto(commit))+ .collect(),+ };+ if response_tx.send(Ok(response)).await.is_err() {+ return;+ }+ }+ }+ })+ .detach();++ Ok(response_rx)+ }+async fn handle_search_commits(this: Entity<Self>,envelope: TypedEnvelope<proto::SearchCommits>,@@ -4413,23 +4514,7 @@ impl Repository {});let (job_sender, state) = (refetch_repo_state)(cx);-- // todo(git_graph_remote): Make this subscription on both remote/local repo- cx.subscribe_self(move |this, event: &RepositoryEvent, _| match event {- RepositoryEvent::HeadChanged | RepositoryEvent::BranchListChanged => {- if this.scan_id > 2 {- this.initial_graph_data.clear();- }- }- RepositoryEvent::StashEntriesChanged => {- if this.scan_id > 2 {- this.initial_graph_data- .retain(|(log_source, _), _| *log_source != LogSource::All);- }- }- _ => {}- })- .detach();+ cx.subscribe_self(Self::handle_subscribe_self).detach();Repository {this: cx.weak_entity(),@@ -4481,6 +4566,8 @@ impl Repository {});let (job_sender, repository_state) = (refetch_repo_state)(cx);+ cx.subscribe_self(Self::handle_subscribe_self).detach();+Self {this: cx.weak_entity(),snapshot,@@ -4501,6 +4588,25 @@ impl Repository {}}+ fn handle_subscribe_self(&mut self, event: &RepositoryEvent, _: &mut Context<Self>) {+ // scan id greater than 2 means the initial snapshot was calculated,+ // otherwise we don't need to refresh the graph state+ match event {+ RepositoryEvent::HeadChanged | RepositoryEvent::BranchListChanged => {+ if self.scan_id > 2 {+ self.initial_graph_data.clear();+ }+ }+ RepositoryEvent::StashEntriesChanged => {+ if self.scan_id > 2 {+ self.initial_graph_data+ .retain(|(log_source, _), _| *log_source != LogSource::All);+ }+ }+ _ => {}+ }+ }+pub fn git_store(&self) -> Option<Entity<GitStore>> {self.git_store.upgrade()}@@ -5121,28 +5227,51 @@ impl Repository {).await}- Ok(RepositoryState::Remote(_)) => {- Err("Git graph is not supported for collab yet".into())+ Ok(RepositoryState::Remote(remote)) => {+ Self::remote_git_graph_data(+ repository.clone(),+ remote,+ log_source.clone(),+ log_order,+ cx,+ )+ .await}Err(e) => Err(SharedString::from(e)),};- if let Err(fetch_task_error) = result {- repository- .update(cx, |repository, _| {- if let Some(data) = repository- .initial_graph_data- .get_mut(&(log_source, log_order))- {- data.error = Some(fetch_task_error);- } else {- debug_panic!(- "This task would be dropped if this entry doesn't exist"- );+ repository+ .update(cx, |repository, cx| {+ if let Some(data) = repository+ .initial_graph_data+ .get_mut(&(log_source.clone(), log_order))+ {+ match &result {+ Ok(()) => {+ cx.emit(RepositoryEvent::GraphEvent(+ (log_source.clone(), log_order),+ GitGraphEvent::FullyLoaded,+ ));+ }+ Err(fetch_task_error) => {+ data.subscribers.retain(|sender| {+ sender.try_send(Err(fetch_task_error.clone())).is_ok()+ });+ data.error = Some(fetch_task_error.clone());+ cx.emit(RepositoryEvent::GraphEvent(+ (log_source.clone(), log_order),+ GitGraphEvent::LoadingError,+ ));+ }}- })- .ok();- }+ data.subscribers.clear();+ } else {+ debug_panic!(+ "This task would be dropped if this entry doesn't exist"+ );+ }+ })+ .log_err();});InitialGitGraphData {@@ -5150,6 +5279,7 @@ impl Repository {error: None,commit_data: Vec::new(),commit_oid_to_index: HashMap::default(),+ subscribers: Vec::new(),}});@@ -5164,6 +5294,47 @@ impl Repository {}}+ async fn append_initial_graph_commits(+ this: &WeakEntity<Self>,+ graph_data_key: &(LogSource, LogOrder),+ initial_graph_commit_data: Vec<Arc<InitialGraphCommitData>>,+ cx: &mut AsyncApp,+ ) {+ this.update(cx, |repository, cx| {+ let graph_data = repository+ .initial_graph_data+ .entry(graph_data_key.clone())+ .and_modify(|graph_data| {+ if !graph_data.subscribers.is_empty() {+ graph_data.subscribers.retain(|sender| {+ sender+ .try_send(Ok(initial_graph_commit_data.clone()))+ .is_ok()+ });+ }++ for commit_data in initial_graph_commit_data {+ graph_data+ .commit_oid_to_index+ .insert(commit_data.sha, graph_data.commit_data.len());+ graph_data.commit_data.push(commit_data);+ }+ cx.emit(RepositoryEvent::GraphEvent(+ graph_data_key.clone(),+ GitGraphEvent::CountUpdated(graph_data.commit_data.len()),+ ));+ });++ match &graph_data {+ Entry::Occupied(_) => {}+ Entry::Vacant(_) => {+ debug_panic!("This task should be dropped if data doesn't exist");+ }+ }+ })+ .log_err();+ }+async fn local_git_graph_data(this: WeakEntity<Self>,backend: Arc<dyn GitRepository>,@@ -5187,34 +5358,52 @@ impl Repository {let graph_data_key = (log_source, log_order);while let Ok(initial_graph_commit_data) = request_rx.recv().await {- this.update(cx, |repository, cx| {- let graph_data = repository- .initial_graph_data- .entry(graph_data_key.clone())- .and_modify(|graph_data| {- for commit_data in initial_graph_commit_data {- graph_data- .commit_oid_to_index- .insert(commit_data.sha, graph_data.commit_data.len());- graph_data.commit_data.push(commit_data);- }- cx.emit(RepositoryEvent::GraphEvent(- graph_data_key.clone(),- GitGraphEvent::CountUpdated(graph_data.commit_data.len()),- ));- });+ Self::append_initial_graph_commits(+ &this,+ &graph_data_key,+ initial_graph_commit_data,+ cx,+ )+ .await;+ }- match &graph_data {- Entry::Occupied(_) => {}- Entry::Vacant(_) => {- debug_panic!("This task should be dropped if data doesn't exist");- }- }+ task.await?;+ Ok(())+ }++ async fn remote_git_graph_data(+ this: WeakEntity<Self>,+ remote: RemoteRepositoryState,+ log_source: LogSource,+ log_order: LogOrder,+ cx: &mut AsyncApp,+ ) -> Result<(), SharedString> {+ let repository_id = this+ .update(cx, |repository, _| repository.id)+ .map_err(|err| SharedString::from(err.to_string()))?;+ let graph_data_key = (log_source.clone(), log_order);+ let mut response = remote+ .client+ .request_stream(proto::GetInitialGraphData {+ project_id: remote.project_id.to_proto(),+ repository_id: repository_id.to_proto(),+ log_source: Some(log_source_to_proto(&log_source)),+ log_order: log_order_to_proto(log_order),})- .ok();+ .await+ .map_err(|err| SharedString::from(err.to_string()))?;++ while let Some(response) = response.next().await {+ let response = response.map_err(|err| SharedString::from(err.to_string()))?;+ let commits = response+ .commits+ .into_iter()+ .map(initial_graph_commit_from_proto)+ .collect::<Result<Vec<_>>>()+ .map_err(|err| SharedString::from(err.to_string()))?;+ Self::append_initial_graph_commits(&this, &graph_data_key, commits, cx).await;}- task.await?;Ok(())}@@ -8249,6 +8438,65 @@ fn log_source_from_proto(log_source: proto::GitLogSource) -> Result<LogSource> {}}+fn log_order_to_proto(log_order: LogOrder) -> i32 {+ match log_order {+ LogOrder::DateOrder => proto::get_initial_graph_data::LogOrder::DateOrder as i32,+ LogOrder::TopoOrder => proto::get_initial_graph_data::LogOrder::TopoOrder as i32,+ LogOrder::AuthorDateOrder => {+ proto::get_initial_graph_data::LogOrder::AuthorDateOrder as i32+ }+ LogOrder::ReverseChronological => {+ proto::get_initial_graph_data::LogOrder::ReverseChronological as i32+ }+ }+}++fn log_order_from_proto(log_order: proto::get_initial_graph_data::LogOrder) -> LogOrder {+ match log_order {+ proto::get_initial_graph_data::LogOrder::DateOrder => LogOrder::DateOrder,+ proto::get_initial_graph_data::LogOrder::TopoOrder => LogOrder::TopoOrder,+ proto::get_initial_graph_data::LogOrder::AuthorDateOrder => LogOrder::AuthorDateOrder,+ proto::get_initial_graph_data::LogOrder::ReverseChronological => {+ LogOrder::ReverseChronological+ }+ }+}++fn initial_graph_commit_to_proto(commit: &InitialGraphCommitData) -> proto::InitialGraphCommit {+ proto::InitialGraphCommit {+ sha: commit.sha.to_string(),+ parents: commit+ .parents+ .iter()+ .map(|parent| parent.to_string())+ .collect(),+ ref_names: commit+ .ref_names+ .iter()+ .map(|ref_name| ref_name.to_string())+ .collect(),+ }+}++fn initial_graph_commit_from_proto(+ commit: proto::InitialGraphCommit,+) -> Result<Arc<InitialGraphCommitData>> {+ let sha = Oid::from_str(&commit.sha)?;+ let mut parents = SmallVec::with_capacity(commit.parents.len());+ for parent in &commit.parents {+ parents.push(Oid::from_str(parent)?);+ }+ Ok(Arc::new(InitialGraphCommitData {+ sha,+ parents,+ ref_names: commit+ .ref_names+ .into_iter()+ .map(SharedString::from)+ .collect(),+ }))+}+fn commit_data_to_proto(commit: &CommitData) -> proto::CommitData {proto::CommitData {sha: commit.sha.to_string(),diff --git a/crates/proto/proto/git.proto b/crates/proto/proto/git.protoindex afea6cf34a3..bf7bbeb4359 100644--- a/crates/proto/proto/git.proto+++ b/crates/proto/proto/git.proto@@ -705,6 +705,30 @@ message GitLogSource {}}+message GetInitialGraphData {+ uint64 project_id = 1;+ uint64 repository_id = 2;+ GitLogSource log_source = 3;++ enum LogOrder {+ DATE_ORDER = 0;+ TOPO_ORDER = 1;+ AUTHOR_DATE_ORDER = 2;+ REVERSE_CHRONOLOGICAL = 3;+ }+ LogOrder log_order = 4;+}++message InitialGraphCommit {+ string sha = 1;+ repeated string parents = 2;+ repeated string ref_names = 3;+}++message GetInitialGraphDataResponse {+ repeated InitialGraphCommit commits = 1;+}+message SearchCommits {uint64 project_id = 1;uint64 repository_id = 2;diff --git a/crates/proto/proto/zed.proto b/crates/proto/proto/zed.protoindex 0c149fb2976..a0fde40a84b 100644--- a/crates/proto/proto/zed.proto+++ b/crates/proto/proto/zed.proto@@ -482,7 +482,9 @@ message Envelope {GetCommitData get_commit_data = 447;GetCommitDataResponse get_commit_data_response = 448;SearchCommits search_commits = 449;- SearchCommitsResponse search_commits_response = 450; // current max+ SearchCommitsResponse search_commits_response = 450;+ GetInitialGraphData get_initial_graph_data = 451;+ GetInitialGraphDataResponse get_initial_graph_data_response = 452; // current max}reserved 87 to 88;diff --git a/crates/proto/src/proto.rs b/crates/proto/src/proto.rsindex 651e11354a9..49b9db0d5c3 100644--- a/crates/proto/src/proto.rs+++ b/crates/proto/src/proto.rs@@ -358,6 +358,8 @@ messages!((GitRepairWorktrees, Background),(GetCommitData, Background),(GetCommitDataResponse, Background),+ (GetInitialGraphData, Background),+ (GetInitialGraphDataResponse, Background),(SearchCommits, Background),(SearchCommitsResponse, Background),(GitWorktreesResponse, Background),@@ -575,6 +577,7 @@ request_messages!((GitEditRef, Ack),(GitRepairWorktrees, Ack),(GetCommitData, GetCommitDataResponse),+ (GetInitialGraphData, GetInitialGraphDataResponse),(SearchCommits, SearchCommitsResponse),(GitCreateWorktree, Ack),(GitRemoveWorktree, Ack),@@ -770,6 +773,7 @@ entity_messages!(GitEditRef,GitRepairWorktrees,GetCommitData,+ GetInitialGraphData,SearchCommits,GitCreateArchiveCheckpoint,GitRestoreArchiveCheckpoint,