Cache `which git` in `uv init` (#12893)

Avoid running `which` multiple times, to be more coherent with the other
git code. In preparation of improving the `uv init` git handling.
This commit is contained in:
konsti 2025-04-15 11:40:08 +02:00 committed by GitHub
parent 30361e59c3
commit 88cd7d619f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 14 deletions

2
Cargo.lock generated
View File

@ -4955,12 +4955,12 @@ dependencies = [
"uv-cache-info", "uv-cache-info",
"uv-cache-key", "uv-cache-key",
"uv-distribution-types", "uv-distribution-types",
"uv-git",
"uv-normalize", "uv-normalize",
"uv-pep440", "uv-pep440",
"uv-pep508", "uv-pep508",
"uv-platform-tags", "uv-platform-tags",
"uv-static", "uv-static",
"which",
] ]
[[package]] [[package]]

View File

@ -21,6 +21,7 @@ uv-cache = { workspace = true }
uv-cache-info = { workspace = true } uv-cache-info = { workspace = true }
uv-cache-key = { workspace = true } uv-cache-key = { workspace = true }
uv-distribution-types = { workspace = true } uv-distribution-types = { workspace = true }
uv-git = { workspace = true }
uv-normalize = { workspace = true } uv-normalize = { workspace = true }
uv-pep440 = { workspace = true } uv-pep440 = { workspace = true }
uv-pep508 = { workspace = true, features = ["schemars"] } uv-pep508 = { workspace = true, features = ["schemars"] }
@ -40,7 +41,6 @@ serde_json = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
url = { workspace = true } url = { workspace = true }
which = { workspace = true }
[dev-dependencies] [dev-dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }

View File

@ -4,6 +4,7 @@ use std::process::{Command, Stdio};
use serde::Deserialize; use serde::Deserialize;
use tracing::debug; use tracing::debug;
use uv_git::GIT;
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum VersionControlError { pub enum VersionControlError {
@ -35,7 +36,7 @@ impl VersionControlSystem {
pub fn init(&self, path: &Path) -> Result<(), VersionControlError> { pub fn init(&self, path: &Path) -> Result<(), VersionControlError> {
match self { match self {
Self::Git => { Self::Git => {
let Ok(git) = which::which("git") else { let Ok(git) = GIT.as_ref() else {
return Err(VersionControlError::GitNotInstalled); return Err(VersionControlError::GitNotInstalled);
}; };
@ -80,17 +81,16 @@ impl VersionControlSystem {
/// Detects the VCS system based on the provided path. /// Detects the VCS system based on the provided path.
pub fn detect(path: &Path) -> Option<Self> { pub fn detect(path: &Path) -> Option<Self> {
// Determine whether the path is inside a Git work tree. // Determine whether the path is inside a Git work tree.
if which::which("git").is_ok_and(|git| { let git = GIT.as_ref().ok()?;
Command::new(git) let exit_status = Command::new(git)
.arg("rev-parse") .arg("rev-parse")
.arg("--is-inside-work-tree") .arg("--is-inside-work-tree")
.current_dir(path) .current_dir(path)
.stdout(Stdio::null()) .stdout(Stdio::null())
.stderr(Stdio::null()) .stderr(Stdio::null())
.status() .status()
.map(|status| status.success()) .ok()?;
.unwrap_or(false) if exit_status.success() {
}) {
return Some(Self::Git); return Some(Self::Git);
} }