Skip to content

Commit 88cd7d6

Browse files
authored
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.
1 parent 30361e5 commit 88cd7d6

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/uv-configuration/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ uv-cache = { workspace = true }
2121
uv-cache-info = { workspace = true }
2222
uv-cache-key = { workspace = true }
2323
uv-distribution-types = { workspace = true }
24+
uv-git = { workspace = true }
2425
uv-normalize = { workspace = true }
2526
uv-pep440 = { workspace = true }
2627
uv-pep508 = { workspace = true, features = ["schemars"] }
@@ -40,7 +41,6 @@ serde_json = { workspace = true }
4041
thiserror = { workspace = true }
4142
tracing = { workspace = true }
4243
url = { workspace = true }
43-
which = { workspace = true }
4444

4545
[dev-dependencies]
4646
anyhow = { workspace = true }

crates/uv-configuration/src/vcs.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::process::{Command, Stdio};
44

55
use serde::Deserialize;
66
use tracing::debug;
7+
use uv_git::GIT;
78

89
#[derive(Debug, thiserror::Error)]
910
pub enum VersionControlError {
@@ -35,7 +36,7 @@ impl VersionControlSystem {
3536
pub fn init(&self, path: &Path) -> Result<(), VersionControlError> {
3637
match self {
3738
Self::Git => {
38-
let Ok(git) = which::which("git") else {
39+
let Ok(git) = GIT.as_ref() else {
3940
return Err(VersionControlError::GitNotInstalled);
4041
};
4142

@@ -80,17 +81,16 @@ impl VersionControlSystem {
8081
/// Detects the VCS system based on the provided path.
8182
pub fn detect(path: &Path) -> Option<Self> {
8283
// Determine whether the path is inside a Git work tree.
83-
if which::which("git").is_ok_and(|git| {
84-
Command::new(git)
85-
.arg("rev-parse")
86-
.arg("--is-inside-work-tree")
87-
.current_dir(path)
88-
.stdout(Stdio::null())
89-
.stderr(Stdio::null())
90-
.status()
91-
.map(|status| status.success())
92-
.unwrap_or(false)
93-
}) {
84+
let git = GIT.as_ref().ok()?;
85+
let exit_status = Command::new(git)
86+
.arg("rev-parse")
87+
.arg("--is-inside-work-tree")
88+
.current_dir(path)
89+
.stdout(Stdio::null())
90+
.stderr(Stdio::null())
91+
.status()
92+
.ok()?;
93+
if exit_status.success() {
9494
return Some(Self::Git);
9595
}
9696

0 commit comments

Comments
 (0)