use std::path::Path;
use std::process::{Command, Output};
#[cfg(windows)]
const NULL_DEVICE: &str = "NUL";
#[cfg(not(windows))]
const NULL_DEVICE: &str = "/dev/null";
pub(crate) fn command(repo: &Path) -> Command {
let mut command = Command::new("git");
for (name, _) in std::env::vars_os() {
if name.to_str().is_some_and(|name| name.starts_with("GIT_")) {
command.env_remove(name);
}
}
command
.arg("--no-pager")
.arg("--no-optional-locks")
.arg("--no-replace-objects")
.arg("-c")
.arg("core.fsmonitor=false")
.arg("-c")
.arg(format!("core.hooksPath={NULL_DEVICE}"))
.arg("-c")
.arg(format!("core.attributesFile={NULL_DEVICE}"))
.arg("-c")
.arg(format!("core.excludesFile={NULL_DEVICE}"))
.arg("-c")
.arg("diff.external=")
.arg("-c")
.arg("submodule.recurse=false")
.arg("-c")
.arg("protocol.file.allow=never")
.arg("-C")
.arg(repo)
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_CONFIG_GLOBAL", NULL_DEVICE)
.env("GIT_OPTIONAL_LOCKS", "0")
.env("GIT_NO_REPLACE_OBJECTS", "1")
.env("GIT_LITERAL_PATHSPECS", "1")
.env("GIT_ATTR_NOSYSTEM", "1")
.env("GIT_TERMINAL_PROMPT", "0")
.env("GIT_PAGER", "cat")
.env("PAGER", "cat")
.env("LC_ALL", "C");
command
}
pub(crate) fn output(repo: &Path, args: &[&str]) -> Result<Output, String> {
command(repo)
.args(args)
.output()
.map_err(|error| format!("run git {}: {error}", args.join(" ")))
}
pub(crate) fn text(repo: &Path, args: &[&str]) -> Result<String, String> {
let output = output(repo, args)?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
return Err(if stderr.is_empty() {
format!("git {} failed with {}", args.join(" "), output.status)
} else {
stderr
});
}
String::from_utf8(output.stdout)
.map(|text| text.trim().to_string())
.map_err(|error| format!("git {} output was not UTF-8: {error}", args.join(" ")))
}