diff --git a/Cargo.lock b/Cargo.lock index 15f2b257..3fac0586 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1718,6 +1718,7 @@ dependencies = [ "js-sys", "miette", "node-semver", + "once_cell", "oro-client", "oro-common", "oro-package-spec", diff --git a/Cargo.toml b/Cargo.toml index 24f4ae58..6ac5e2b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,6 +82,7 @@ maplit = "1.0.2" miette = "5.5.0" node-semver = "2.1.0" nom = "7.1.3" +once_cell = "1.17.1" percent-encoding = "2.1.0" pretty_assertions = "1.3.0" proc-macro2 = "1.0.18" diff --git a/crates/nassun/Cargo.toml b/crates/nassun/Cargo.toml index b7747165..83a30154 100644 --- a/crates/nassun/Cargo.toml +++ b/crates/nassun/Cargo.toml @@ -22,6 +22,7 @@ dashmap = { workspace = true } futures = { workspace = true } miette = { workspace = true } node-semver = { workspace = true } +once_cell = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } ssri = { workspace = true } diff --git a/crates/nassun/src/fetch/git.rs b/crates/nassun/src/fetch/git.rs index 36caa9df..956566fd 100644 --- a/crates/nassun/src/fetch/git.rs +++ b/crates/nassun/src/fetch/git.rs @@ -1,8 +1,9 @@ use std::path::{Path, PathBuf}; use async_process::{Command, Stdio}; -use async_std::sync::{Arc, Mutex}; +use async_std::sync::Arc; use async_trait::async_trait; +use once_cell::sync::OnceCell; use oro_client::{self, OroClient}; use oro_common::{CorgiPackument, CorgiVersionMetadata, Packument, VersionMetadata}; use oro_package_spec::{GitInfo, PackageSpec}; @@ -19,7 +20,7 @@ use crate::tarball::Tarball; pub(crate) struct GitFetcher { client: OroClient, dir_fetcher: DirFetcher, - git: Arc>>, + git: OnceCell, } impl GitFetcher { @@ -27,7 +28,7 @@ impl GitFetcher { Self { client, dir_fetcher: DirFetcher::new(), - git: Arc::new(Mutex::new(None)), + git: OnceCell::new(), } } @@ -90,15 +91,10 @@ impl GitFetcher { committish: &Option, ) -> Result<()> { let repo = repo.as_ref(); - let git = if let Some(git) = self.git.lock().await.as_ref() { - git.clone() - } else { - let git = which::which("git").map_err(NassunError::WhichGit)?; - let mut selfgit = self.git.lock().await; - *selfgit = Some(git.clone()); - git - }; - Command::new(&git) + let git = self + .git + .get_or_try_init(|| which::which("git").map_err(NassunError::WhichGit))?; + Command::new(git) .arg("clone") .arg(repo) .arg("package") @@ -117,7 +113,7 @@ impl GitFetcher { } })?; if let Some(committish) = committish { - Command::new(&git) + Command::new(git) .arg("checkout") .arg(committish) .current_dir(dir.join("package"))