use std::fs;
use std::path::Path;
pub(super) fn stamp(path: &Path, unix: i64) -> Result<(), String> {
let when = std::time::UNIX_EPOCH + std::time::Duration::from_secs(unix.unsigned_abs());
let times = fs::FileTimes::new().set_modified(when);
fs::File::options()
.write(true)
.open(path)
.and_then(|f| f.set_times(times))
.map_err(|e| format!("stamp {}: {e}", path.display()))
}
pub(super) fn mkdir(path: &Path) -> Result<(), String> {
fs::create_dir_all(path).map_err(|e| format!("create {}: {e}", path.display()))
}
pub(super) fn write(path: &Path, body: &str) -> Result<(), String> {
if let Some(parent) = path.parent() {
mkdir(parent)?;
}
fs::write(path, body).map_err(|e| format!("write {}: {e}", path.display()))
}
pub(super) fn display(path: &Path) -> String {
path.display().to_string()
}
pub(super) fn git(dir: &Path, args: &[&str], when: Option<i64>) -> Result<(), String> {
let mut cmd = crate::git_env::git();
cmd.env("GIT_CONFIG_GLOBAL", "/dev/null");
cmd.env("GIT_CONFIG_SYSTEM", "/dev/null");
cmd.env("GIT_AUTHOR_NAME", "yog fixture");
cmd.env("GIT_AUTHOR_EMAIL", "fixture@yog.invalid");
cmd.env("GIT_COMMITTER_NAME", "yog fixture");
cmd.env("GIT_COMMITTER_EMAIL", "fixture@yog.invalid");
if let Some(unix) = when {
let stamp = format!("@{unix} +0000");
cmd.env("GIT_AUTHOR_DATE", &stamp);
cmd.env("GIT_COMMITTER_DATE", &stamp);
}
let status = crate::git_env::status(cmd.arg("-C").arg(dir).args(args))
.map_err(|e| format!("git {args:?}: {e}"))?;
if status.success() {
Ok(())
} else {
Err(format!("git {args:?}: {status}"))
}
}
#[cfg(test)]
mod tests;