Skip to content

Commit

Permalink
fix(git): use once_cell instead of mutexes for git cache path
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Mar 18, 2023
1 parent b66a66e commit 5961dfb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions crates/nassun/Cargo.toml
Expand Up @@ -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 }
Expand Down
22 changes: 9 additions & 13 deletions 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};
Expand All @@ -19,15 +20,15 @@ use crate::tarball::Tarball;
pub(crate) struct GitFetcher {
client: OroClient,
dir_fetcher: DirFetcher,
git: Arc<Mutex<Option<PathBuf>>>,
git: OnceCell<PathBuf>,
}

impl GitFetcher {
pub(crate) fn new(client: OroClient) -> Self {
Self {
client,
dir_fetcher: DirFetcher::new(),
git: Arc::new(Mutex::new(None)),
git: OnceCell::new(),
}
}

Expand Down Expand Up @@ -90,15 +91,10 @@ impl GitFetcher {
committish: &Option<String>,
) -> 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")
Expand All @@ -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"))
Expand Down

0 comments on commit 5961dfb

Please sign in to comment.