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-key",
"uv-distribution-types",
"uv-git",
"uv-normalize",
"uv-pep440",
"uv-pep508",
"uv-platform-tags",
"uv-static",
"which",
]
[[package]]

View file

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

View file

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