From 7b29e1a5b12826624468d871f32f83b01519c010 Mon Sep 17 00:00:00 2001
From: Jo <10510431+j178@users.noreply.github.com>
Date: Thu, 16 Jul 2026 16:52:21 +0800
Subject: [PATCH] Scope synthetic GIT_WORK_TREE to git commands
---
crates/prek/src/cli/run/keeper.rs | 9 ++-
crates/prek/src/cli/update/repository.rs | 42 +++++------
crates/prek/src/git.rs | 84 ++++++++++++++++++----
crates/prek/src/languages/golang/golang.rs | 9 ++-
crates/prek/src/languages/python/python.rs | 3 +-
crates/prek/src/languages/rust/rust.rs | 9 +--
crates/prek/src/main.rs | 19 ++---
crates/prek/src/process.rs | 8 ---
crates/prek/tests/hook_impl.rs | 35 +++++++++
9 files changed, 152 insertions(+), 66 deletions(-)
diff --git a/crates/prek/src/cli/run/keeper.rs b/crates/prek/src/cli/run/keeper.rs
index 4484bbe95..fd326a5e7 100644
--- a/crates/prek/src/cli/run/keeper.rs
+++ b/crates/prek/src/cli/run/keeper.rs
@@ -59,7 +59,8 @@ impl IntentToAddRestorer {
fn restore(&self) -> Result<()> {
// Restore the intent-to-add changes.
if !self.0.is_empty() {
- Command::new(GIT.as_ref()?)
+ let mut cmd = Command::new(GIT.as_ref()?);
+ git::apply_git_work_tree(&mut cmd)
.arg("add")
.arg("--intent-to-add")
.arg("--")
@@ -154,7 +155,8 @@ impl UnstagedChangesRestorer {
}
fn checkout_working_tree(root: &Path) -> Result<()> {
- let output = Command::new(GIT.as_ref()?)
+ let mut cmd = Command::new(GIT.as_ref()?);
+ let output = git::apply_git_work_tree(&mut cmd)
.arg("-c")
.arg("submodule.recurse=0")
.arg("checkout")
@@ -173,7 +175,8 @@ impl UnstagedChangesRestorer {
}
fn git_apply(patch: &Path) -> Result<()> {
- let output = Command::new(GIT.as_ref()?)
+ let mut cmd = Command::new(GIT.as_ref()?);
+ let output = git::apply_git_work_tree(&mut cmd)
.arg("apply")
.arg("--whitespace=nowarn")
.arg(patch)
diff --git a/crates/prek/src/cli/update/repository.rs b/crates/prek/src/cli/update/repository.rs
index 3a6cdae5c..1bef19323 100644
--- a/crates/prek/src/cli/update/repository.rs
+++ b/crates/prek/src/cli/update/repository.rs
@@ -14,6 +14,7 @@ use semver::Version;
use tracing::{debug, trace};
use crate::cli::update::{CommitPresence, RevisionSelection, SkippedDowngrade, TagTimestamp};
+use crate::git::GitCommandExt;
use crate::{config, git};
/// Initializes a temporary git repo and fetches the remote HEAD plus tags.
@@ -27,7 +28,7 @@ pub(super) async fn setup_and_fetch_repo(repo_url: &str, repo_path: &Path) -> Re
.arg("--filter=blob:none")
.arg("--tags")
.current_dir(repo_path)
- .remove_git_envs()
+ .isolate_from_git_env()
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
@@ -43,7 +44,7 @@ pub(super) async fn resolve_revision_to_commit(repo_path: &Path, rev: &str) -> R
.arg(format!("{rev}^{{}}"))
.check(true)
.current_dir(repo_path)
- .remove_git_envs()
+ .isolate_from_git_env()
.output()
.await?;
@@ -83,7 +84,7 @@ pub(super) async fn is_commit_present(repo_path: &Path, commit: &str) -> Result<
.env(EnvVars::LC_ALL, "C")
.check(false)
.current_dir(repo_path)
- .remove_git_envs()
+ .isolate_from_git_env()
.stdout(Stdio::null())
.output()
.await?;
@@ -127,7 +128,7 @@ pub(super) async fn resolve_bleeding_edge(repo_path: &Path) -> Result