use std::io::Write;
use std::path::Path;
use std::process::{Command, Stdio};
fn git(dir: &Path) -> Command {
let mut cmd = Command::new("git");
cmd.current_dir(dir);
cmd.env("GIT_CONFIG_GLOBAL", dir.join("absent-global-gitconfig"));
cmd.env("GIT_CONFIG_SYSTEM", dir.join("absent-system-gitconfig"));
crate::gitenv::insulate_repo_location(&mut cmd);
cmd
}
pub(crate) fn repo_with_file(content: &str) -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("f"), content).unwrap();
let status = git(dir.path()).arg("init").arg("-q").status().unwrap();
assert!(status.success(), "git init failed in {:?}", dir.path());
dir
}
pub(crate) fn apply_check(diff_bytes: &[u8], dir: &Path) -> bool {
let mut child = git(dir)
.arg("apply")
.arg("--check")
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.unwrap();
let mut stdin = child.stdin.take().expect("stdin was configured as piped");
std::thread::scope(|scope| {
scope.spawn(move || stdin.write_all(diff_bytes));
child.wait_with_output().unwrap()
})
.status
.success()
}
pub(crate) fn applies_to_file(diff_bytes: &[u8], content: &str) -> bool {
let dir = repo_with_file(content);
apply_check(diff_bytes, dir.path())
}