use std::ffi::{OsStr, OsString};
use std::io::{self, Write};
use std::path::Path;
use std::process::{Command, Stdio};
use crate::types::project::GitUrl;
use crate::types::resolve::FetchError;
const REPOSITORY_VARIABLES: [&str; 7] = [
"GIT_DIR",
"GIT_WORK_TREE",
"GIT_INDEX_FILE",
"GIT_OBJECT_DIRECTORY",
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
"GIT_COMMON_DIR",
"GIT_NAMESPACE",
];
pub(super) struct GitRun<'a> {
program: &'a OsStr,
repo: Option<&'a Path>,
subcommand: &'static str,
args: Vec<OsString>,
operands: Vec<OsString>,
stdin: Option<&'a [u8]>,
}
impl<'a> GitRun<'a> {
pub(super) const fn new(program: &'a OsStr, subcommand: &'static str) -> Self {
Self {
program,
repo: None,
subcommand,
args: Vec::new(),
operands: Vec::new(),
stdin: None,
}
}
pub(super) const fn repo(mut self, repo: &'a Path) -> Self {
self.repo = Some(repo);
self
}
pub(super) fn args<I, S>(mut self, args: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.args
.extend(args.into_iter().map(|arg| arg.as_ref().to_owned()));
self
}
pub(super) fn url(self, url: &GitUrl) -> Self {
self.operand(url.as_str())
}
pub(super) fn operand(mut self, operand: impl AsRef<OsStr>) -> Self {
self.operands.push(operand.as_ref().to_owned());
self
}
pub(super) const fn stdin(mut self, bytes: &'a [u8]) -> Self {
self.stdin = Some(bytes);
self
}
pub(super) fn argv(&self) -> Vec<OsString> {
let mut argv: Vec<OsString> = vec!["-c".into(), "core.autocrlf=false".into()];
if let Some(repo) = self.repo {
argv.push("-C".into());
argv.push(repo.into());
}
argv.push(self.subcommand.into());
argv.extend(self.args.iter().cloned());
if !self.operands.is_empty() {
argv.push("--".into());
argv.extend(self.operands.iter().cloned());
}
argv
}
fn command(&self) -> Command {
let mut command = Command::new(self.program);
for variable in REPOSITORY_VARIABLES {
command.env_remove(variable);
}
command
.args(self.argv())
.env("GIT_TERMINAL_PROMPT", "0")
.stdin(if self.stdin.is_some() {
Stdio::piped()
} else {
Stdio::null()
})
.stdout(Stdio::piped())
.stderr(Stdio::piped());
command
}
pub(super) fn run(&self) -> Result<Vec<u8>, FetchError> {
let mut child = self.command().spawn().map_err(spawn_failure)?;
let pipe = child.stdin.take();
let output = std::thread::scope(|scope| {
if let (Some(mut pipe), Some(bytes)) = (pipe, self.stdin) {
scope.spawn(move || {
let _ = pipe.write_all(bytes);
});
}
child.wait_with_output()
})
.map_err(FetchError::GitSpawn)?;
if output.status.success() {
Ok(output.stdout)
} else {
Err(FetchError::GitFailed {
subcommand: self.subcommand.to_owned(),
stderr: String::from_utf8_lossy(&output.stderr).trim().to_owned(),
})
}
}
}
fn spawn_failure(error: io::Error) -> FetchError {
if error.kind() == io::ErrorKind::NotFound {
FetchError::GitNotFound {
needed_by: Vec::new(),
}
} else {
FetchError::GitSpawn(error)
}
}
#[cfg(test)]
mod tests {
use std::ffi::OsString;
use super::*;
use crate::process::git::testing::{CRLF, SourceRepo, TestResult};
use crate::types::report::{Diagnostic, Location};
const GIT: &str = "git";
const STDIN_IDS: usize = 20_000;
fn os(args: &[&str]) -> Vec<OsString> {
args.iter().map(OsString::from).collect()
}
#[test]
fn argv_puts_autocrlf_first_and_the_url_after_double_dash() -> TestResult {
let repo = Path::new("/r");
let run = GitRun::new(OsStr::new(GIT), "fetch")
.repo(repo)
.args(["--no-tags", "--depth", "1"])
.url(&GitUrl::new("u")?)
.operand("x");
assert_eq!(
run.argv(),
os(&[
"-c",
"core.autocrlf=false",
"-C",
"/r",
"fetch",
"--no-tags",
"--depth",
"1",
"--",
"u",
"x"
])
);
Ok(())
}
#[test]
fn argv_has_no_double_dash_without_operands() {
let run = GitRun::new(OsStr::new(GIT), "init").args(["--bare", "-q"]);
assert_eq!(
run.argv(),
os(&["-c", "core.autocrlf=false", "init", "--bare", "-q"])
);
}
#[test]
fn run_sets_git_terminal_prompt_off() {
let command = GitRun::new(OsStr::new(GIT), "init").command();
let prompt = command
.get_envs()
.find(|(key, _)| *key == OsStr::new("GIT_TERMINAL_PROMPT"))
.and_then(|(_, value)| value);
assert_eq!(prompt, Some(OsStr::new("0")));
}
#[test]
fn run_clears_the_variables_that_point_git_at_a_repository() {
let command = GitRun::new(OsStr::new(GIT), "fetch").command();
let envs: Vec<_> = command.get_envs().collect();
for key in REPOSITORY_VARIABLES {
assert!(
envs.contains(&(OsStr::new(key), None)),
"{key} is not removed"
);
}
}
#[test]
fn a_missing_program_is_git_not_found() -> TestResult {
let error = GitRun::new(OsStr::new("/nonexistent/dir/git"), "init")
.run()
.err();
let Some(FetchError::GitNotFound { needed_by }) = &error else {
return Err(format!("expected GitNotFound, got {error:?}").into());
};
assert!(needed_by.is_empty());
Ok(())
}
#[test]
fn a_failing_command_carries_subcommand_and_trimmed_stderr() -> TestResult {
let url = GitUrl::new("file:///nope")?;
let error = GitRun::new(OsStr::new(GIT), "ls-remote")
.url(&url)
.run()
.err();
let Some(FetchError::GitFailed { subcommand, stderr }) = &error else {
return Err(format!("expected GitFailed, got {error:?}").into());
};
assert_eq!(subcommand, "ls-remote");
assert!(
stderr.starts_with("fatal: '/nope' does not appear to be a git repository"),
"{stderr}"
);
assert!(!stderr.ends_with('\n'));
Ok(())
}
#[test]
fn a_spawn_error_other_than_not_found_is_a_git_spawn_refusal() -> TestResult {
let dir = tempfile::tempdir()?;
let error = GitRun::new(dir.path().as_os_str(), "init")
.run()
.err()
.ok_or("expected a failure")?;
let FetchError::GitSpawn(io) = &error else {
return Err(format!("expected GitSpawn, got {error:?}").into());
};
assert_ne!(io.kind(), io::ErrorKind::NotFound);
let expected = format!("agentstacks.toml: git: {io}");
let shown = Diagnostic::new(Location::file("agentstacks.toml"), error).to_string();
assert_eq!(shown, expected);
Ok(())
}
#[test]
fn stdin_is_fed_while_output_is_read() -> TestResult {
let source = SourceRepo::new()?;
let path = source.path();
let line = format!("{}\n", source.crlf_blob);
let input = line.repeat(STDIN_IDS);
let output = GitRun::new(OsStr::new(GIT), "cat-file")
.repo(&path)
.args(["--batch"])
.stdin(input.as_bytes())
.run()?;
let mut frame = format!("{} blob {}\n", source.crlf_blob, CRLF.len()).into_bytes();
frame.extend_from_slice(CRLF);
frame.push(b'\n');
assert_eq!(output, frame.repeat(STDIN_IDS));
Ok(())
}
}