use std::ffi::OsStr;
use std::process::Command;
pub(crate) struct Git(Command);
impl Git {
pub(crate) fn new() -> Self {
Self(crate::proc::command("git"))
}
pub(crate) fn at(mut self, dir: impl AsRef<OsStr>) -> Self {
self.0.arg("-C").arg(dir);
self
}
pub(crate) fn config(mut self, key: &str, value: &str) -> Self {
self.0.arg("-c").arg(format!("{key}={value}"));
self
}
pub(crate) fn without_optional_locks(mut self) -> Self {
self.0.env("GIT_OPTIONAL_LOCKS", "0");
self
}
pub(crate) fn never_prompting(mut self) -> Self {
self.0
.env("GIT_TERMINAL_PROMPT", "0")
.stdin(std::process::Stdio::null());
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
unsafe {
self.0.pre_exec(|| {
libc::setsid();
Ok(())
});
}
}
self
}
pub(crate) fn arg(mut self, arg: impl AsRef<OsStr>) -> Self {
self.0.arg(arg);
self
}
pub(crate) fn outside_inherited_repo(mut self) -> Self {
for name in [
"GIT_DIR",
"GIT_WORK_TREE",
"GIT_INDEX_FILE",
"GIT_COMMON_DIR",
] {
self.0.env_remove(name);
}
self
}
pub(crate) fn args<I, S>(mut self, args: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.0.args(args);
self
}
pub(crate) fn envs<I, K, V>(mut self, vars: I) -> Self
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.0.envs(vars);
self
}
pub(crate) fn into_command(self) -> Command {
self.0
}
}