use std::path::{Path, PathBuf};
use async_channel::Receiver;
use futures_lite::io::AsyncWriteExt as _;
use super::{CommitLedgerError, NULL_DEVICE, SubagentGrantError};
const MINIMUM_VERSION: (u32, u32) = (2, 40);
#[derive(Debug, Clone, Copy)]
pub(super) struct Quarantine<'a> {
pub objects: &'a Path,
pub alternate: &'a Path,
}
pub(super) struct HostGit<'a> {
git_dir: Option<&'a Path>,
quarantine: Option<Quarantine<'a>>,
args: &'a [&'a str],
keep_running: bool,
}
impl<'a> HostGit<'a> {
pub(super) const fn in_repository(git_dir: &'a Path, args: &'a [&'a str]) -> Self {
Self {
git_dir: Some(git_dir),
quarantine: None,
args,
keep_running: false,
}
}
pub(super) const fn finish_when_dropped(mut self) -> Self {
self.keep_running = true;
self
}
pub(super) const fn through(mut self, quarantine: Quarantine<'a>) -> Self {
self.quarantine = Some(quarantine);
self
}
fn command(&self) -> async_process::Command {
let mut command = async_process::Command::new("git");
for (key, _) in std::env::vars_os() {
if key.to_str().is_some_and(|key| key.starts_with("GIT_")) {
command.env_remove(key);
}
}
command
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_CONFIG_GLOBAL", NULL_DEVICE)
.env("GIT_NO_LAZY_FETCH", "1")
.env("GIT_NO_REPLACE_OBJECTS", "1")
.env("GIT_OPTIONAL_LOCKS", "0")
.env("GIT_TERMINAL_PROMPT", "0");
if let Some(quarantine) = self.quarantine {
command
.env("GIT_OBJECT_DIRECTORY", quarantine.objects)
.env("GIT_ALTERNATE_OBJECT_DIRECTORIES", quarantine.alternate);
}
if let Some(git_dir) = self.git_dir {
command.current_dir(git_dir).arg("--git-dir").arg(git_dir);
}
command
.args([
"-c",
"log.showSignature=false",
"-c",
"core.fsmonitor=false",
"-c",
])
.arg(format!("core.hooksPath={NULL_DEVICE}"))
.args(self.args)
.stdout(async_process::Stdio::piped())
.stderr(async_process::Stdio::piped())
.kill_on_drop(!self.keep_running);
command
}
fn repository(&self) -> PathBuf {
self.git_dir.map(Path::to_path_buf).unwrap_or_default()
}
pub(super) async fn output(
&self,
stdin: Receiver<Vec<u8>>,
) -> Result<std::process::Output, CommitLedgerError> {
let spawn_error = |source| CommitLedgerError::Spawn {
repository: self.repository(),
source,
};
let mut command = self.command();
command.stdin(async_process::Stdio::piped());
let mut child = command.spawn().map_err(spawn_error)?;
let mut input = child.stdin.take();
let feed = async move {
let Some(input) = input.as_mut() else {
return Ok(());
};
while let Ok(chunk) = stdin.recv().await {
input.write_all(&chunk).await?;
}
input.flush().await
};
let (written, output) = futures::join!(feed, child.output());
let output = output.map_err(spawn_error)?;
match written {
Err(error) if error.kind() != std::io::ErrorKind::BrokenPipe => Err(spawn_error(error)),
_ => Ok(output),
}
}
pub(super) async fn succeed(
&self,
stdin: Vec<u8>,
) -> Result<std::process::Output, CommitLedgerError> {
let output = self.output(input(stdin)).await?;
if output.status.success() {
Ok(output)
} else {
Err(self.failed(&output))
}
}
pub(super) fn failed(&self, output: &std::process::Output) -> CommitLedgerError {
CommitLedgerError::Git {
repository: self.repository(),
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
}
}
}
pub(super) fn input(data: Vec<u8>) -> Receiver<Vec<u8>> {
let (tx, rx) = async_channel::bounded(1);
if !data.is_empty() {
tx.try_send(data)
.expect("an empty channel with room for one chunk takes it");
}
rx
}
pub(super) async fn git(git_dir: &Path, args: &[&str]) -> Result<String, CommitLedgerError> {
let output = HostGit::in_repository(git_dir, args)
.succeed(Vec::new())
.await?;
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}
pub(super) async fn require_supported_version() -> Result<(), SubagentGrantError> {
let output = HostGit {
git_dir: None,
quarantine: None,
args: &["--version"],
keep_running: false,
}
.succeed(Vec::new())
.await?;
check_version(String::from_utf8_lossy(&output.stdout).trim())
}
pub(super) fn check_version(printed: &str) -> Result<(), SubagentGrantError> {
let version = printed
.strip_prefix("git version ")
.and_then(|rest| rest.split_whitespace().next())
.and_then(|version| {
let mut parts = version.split('.');
let major = parts.next()?.parse::<u32>().ok()?;
let minor = parts.next()?.parse::<u32>().ok()?;
Some((major, minor))
});
match version {
Some(version) if version >= MINIMUM_VERSION => Ok(()),
_ => Err(SubagentGrantError::GitTooOld {
found: printed.to_string(),
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn git_before_2_40_is_refused_naming_the_version_found() {
for old in [
"git version 2.39.4",
"git version 1.9.5",
"git version 2.7.4",
] {
let error = check_version(old).expect_err("option no-deref needs git 2.40");
assert!(
matches!(&error, SubagentGrantError::GitTooOld { found } if found == old),
"{error}"
);
assert!(error.to_string().contains(old), "{error}");
}
for fine in [
"git version 2.40.0",
"git version 2.54.0 (Apple Git-157)",
"git version 3.0.1",
] {
check_version(fine).expect("a supported git");
}
assert!(matches!(
check_version("not git"),
Err(SubagentGrantError::GitTooOld { .. })
));
}
}