Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
git: Reduce number of spawned git processes when retrieving remote URLs (#59053)
Lukas Geigere770c941878b
diff --git a/crates/fs/src/fake_git_repo.rs b/crates/fs/src/fake_git_repo.rsindex 3a2c0980983..d8986cd2416 100644--- a/crates/fs/src/fake_git_repo.rs+++ b/crates/fs/src/fake_git_repo.rs@@ -221,16 +221,9 @@ impl GitRepository for FakeGitRepository {})}- fn remote_url(&self, name: &str) -> BoxFuture<'_, Option<String>> {- let name = name.to_string();- let fut = self.with_state_async(false, move |state| {- state- .remotes- .get(&name)- .context("remote not found")- .cloned()- });- async move { fut.await.ok() }.boxed()+ fn remote_urls(&self) -> BoxFuture<'_, HashMap<String, String>> {+ let fut = self.with_state_async(false, |state| Ok(state.remotes.clone()));+ async move { fut.await.unwrap_or_default() }.boxed()}fn diff_tree(&self, _request: DiffTreeType) -> BoxFuture<'_, Result<TreeDiff>> {diff --git a/crates/git/src/repository.rs b/crates/git/src/repository.rsindex c4849f9f0c5..1aa78eef38d 100644--- a/crates/git/src/repository.rs+++ b/crates/git/src/repository.rs@@ -798,7 +798,14 @@ pub trait GitRepository: Send + Sync {) -> BoxFuture<'_, anyhow::Result<()>>;/// Returns the URL of the remote with the given name.- fn remote_url(&self, name: &str) -> BoxFuture<'_, Option<String>>;+ fn remote_url(&self, name: &str) -> BoxFuture<'_, Option<String>> {+ let name = name.to_string();+ let fut = self.remote_urls();+ async move { fut.await.remove(&name) }.boxed()+ }++ /// Returns the URL of all remotes.+ fn remote_urls(&self) -> BoxFuture<'_, HashMap<String, String>>;/// Resolve a list of refs to SHAs.fn revparse_batch(&self, revs: Vec<String>) -> BoxFuture<'_, Result<Vec<Option<String>>>>;@@ -1687,26 +1694,21 @@ impl GitRepository for RealGitRepository {.boxed()}- fn remote_url(&self, name: &str) -> BoxFuture<'_, Option<String>> {+ fn remote_urls(&self) -> BoxFuture<'_, HashMap<String, String>> {let git = self.git_binary();- let name = name.to_owned();self.executor.spawn(async move {- let output = git- .build_command(&["remote", "get-url", &name])- .output()- .await- .log_err()?;- if !output.status.success() {- return None;- }- let url = String::from_utf8(output.stdout).ok()?;- let url = url.trim();- if url.is_empty() {- None- } else {- Some(url.to_string())+ let mut urls = HashMap::default();+ if let Ok(stdout) = git.run(&["remote", "-v"]).await {+ for line in stdout.lines() {+ if let Some(line) = line.strip_suffix(" (fetch)")+ && let Some((name, url)) = line.split_once(char::is_whitespace)+ {+ urls.insert(name.to_string(), url.trim_start().to_string());+ }+ }}+ urls}).boxed()}@@ -5200,4 +5202,53 @@ mod tests {.boxed()}}++ #[gpui::test]+ async fn test_remote_urls(cx: &mut TestAppContext) {+ disable_git_global_config();+ cx.executor().allow_parking();++ let temp_dir = tempfile::tempdir().unwrap();+ let repo_dir = temp_dir.path().join("repo");+ std::fs::create_dir_all(&repo_dir).unwrap();++ git_init_repo(&repo_dir);++ let repo = RealGitRepository::new(+ &repo_dir.join(".git"),+ None,+ Some("git".into()),+ cx.executor(),+ )+ .unwrap();++ let git = repo.git_binary();+ git.run(&[+ "remote",+ "add",+ "origin",+ "https://github.com/zed-industries/zed.git",+ ])+ .await+ .unwrap();+ git.run(&[+ "remote",+ "add",+ "upstream",+ "/Users/user/My Projects/upstream.git",+ ])+ .await+ .unwrap();++ let remote_urls = repo.remote_urls().await;+ assert_eq!(remote_urls.len(), 2);+ assert_eq!(+ remote_urls.get("origin").unwrap(),+ "https://github.com/zed-industries/zed.git"+ );+ assert_eq!(+ remote_urls.get("upstream").unwrap(),+ "/Users/user/My Projects/upstream.git"+ );+ }}diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rsindex 117433e47ec..ef67a89b69a 100644--- a/crates/project/src/git_store.rs+++ b/crates/project/src/git_store.rs@@ -9915,8 +9915,9 @@ async fn compute_snapshot(.filter(|wt| wt.path != *work_directory_abs_path).collect();- let remote_origin_url = backend.remote_url("origin").await;- let remote_upstream_url = backend.remote_url("upstream").await;+ let mut remote_urls = backend.remote_urls().await;+ let remote_origin_url = remote_urls.remove("origin");+ let remote_upstream_url = remote_urls.remove("upstream");log::debug!("fetched remotes");