use std::ffi::OsString;
use std::path::Path;
use std::process::{Command, Stdio};
use nibkeep_node::{GitRepository, ToClone};
pub fn clear_variables(git: &mut Command) {
for variable in [
"GIT_DIR",
"GIT_WORK_TREE",
"GIT_INDEX_FILE",
"GIT_OBJECT_DIRECTORY",
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
"GIT_COMMON_DIR",
"GIT_NAMESPACE",
"GIT_PREFIX",
"GIT_CEILING_DIRECTORIES",
"GIT_DISCOVERY_ACROSS_FILESYSTEM",
"GIT_CONFIG",
"GIT_CONFIG_PARAMETERS",
"GIT_CONFIG_COUNT",
"GIT_SHALLOW_FILE",
"GIT_GRAFT_FILE",
"GIT_REPLACE_REF_BASE",
"GIT_NO_REPLACE_OBJECTS",
"GIT_IMPLICIT_WORK_TREE",
"GIT_QUARANTINE_PATH",
"GIT_INTERNAL_SUPER_PREFIX",
] {
git.env_remove(variable);
}
}
#[derive(Clone, Debug)]
pub struct Git {
program: OsString,
variables: Vec<(OsString, Option<OsString>)>,
}
impl Git {
#[must_use]
pub fn person() -> Self {
let mut git = Command::new("git");
clear_variables(&mut git);
Self::from_template(&git)
}
#[must_use]
pub fn from_template(template: &Command) -> Self {
Self {
program: template.get_program().to_owned(),
variables: template
.get_envs()
.map(|(key, value)| (key.to_owned(), value.map(ToOwned::to_owned)))
.collect(),
}
}
fn command(&self) -> Command {
let mut command = Command::new(&self.program);
for (key, value) in &self.variables {
match value {
Some(value) => command.env(key, value),
None => command.env_remove(key),
};
}
command
}
fn run(&self, folder: &Path, arguments: &[&str]) -> Option<String> {
let mut git = self.command();
git.arg("-C").arg(folder).args(arguments);
if let Some(parent) = folder.parent() {
git.env("GIT_CEILING_DIRECTORIES", parent);
}
let output = git
.env("GIT_OPTIONAL_LOCKS", "0")
.env("GIT_TERMINAL_PROMPT", "0")
.stdin(Stdio::null())
.stderr(Stdio::null())
.output()
.ok()?;
output
.status
.success()
.then(|| String::from_utf8_lossy(&output.stdout).trim().to_owned())
}
#[must_use]
pub fn describe(&self, root: &Path, paths: &[String]) -> Vec<GitRepository> {
paths
.iter()
.filter_map(|path| self.describe_one(&root.join(path), path))
.collect()
}
#[must_use]
pub fn describe_one(&self, folder: &Path, path: &str) -> Option<GitRepository> {
if !self.holds_own(folder) {
return None;
}
let mut remotes: Vec<(String, String)> = Vec::new();
let listed = self
.run(
folder,
&["config", "--local", "--get-regexp", r"^remote\..*\.url$"],
)
.unwrap_or_default();
for line in listed.lines() {
if let Some((key, address)) = line.split_once(' ')
&& let Some(name) = key
.strip_prefix("remote.")
.and_then(|rest| rest.strip_suffix(".url"))
&& !remotes.iter().any(|(known, _)| known == name)
{
remotes.push((name.to_owned(), address.to_owned()));
}
}
let branch = self
.run(folder, &["symbolic-ref", "--quiet", "--short", "HEAD"])
.filter(|branch| !branch.is_empty());
let upstream = branch
.as_ref()
.and_then(|branch| {
self.run(
folder,
&["config", "--get", &format!("branch.{branch}.remote")],
)
})
.filter(|remote| remotes.iter().any(|(name, _)| name == remote));
Some(GitRepository {
path: path.to_owned(),
remotes,
branch,
upstream,
commit: self.head(folder),
})
}
#[must_use]
pub fn work_only_here(&self, folder: &Path, pinned: Option<&str>) -> Vec<String> {
let mut work = Vec::new();
if !self.holds_own(folder) {
return work;
}
let has_remote = self
.run(folder, &["remote"])
.is_some_and(|remotes| !remotes.is_empty());
if self
.run(folder, &["status", "--porcelain"])
.is_some_and(|changes| !changes.is_empty())
{
work.push("changes not committed".to_owned());
}
if !has_remote {
work.push("no remote at all".to_owned());
} else if let Some(count) = self
.run(
folder,
&["rev-list", "--count", "--branches", "--not", "--remotes"],
)
.and_then(|count| count.parse::<u64>().ok())
.filter(|count| *count > 0)
{
let commits = if count == 1 { "commit" } else { "commits" };
work.push(format!("{count} {commits} not pushed"));
}
if self
.run(folder, &["stash", "list"])
.is_some_and(|stashes| !stashes.is_empty())
{
work.push("stashes".to_owned());
}
if let Some(pinned) = pinned
&& has_remote
&& self
.run(folder, &["cat-file", "-e", &format!("{pinned}^{{commit}}")])
.is_some()
&& self
.run(folder, &["branch", "--remotes", "--contains", pinned])
.is_none_or(|branches| branches.is_empty())
{
work.push("the pinned commit, which no remote has".to_owned());
}
work
}
#[must_use]
pub fn head(&self, folder: &Path) -> Option<String> {
if !self.holds_own(folder) {
return None;
}
self.run(folder, &["rev-parse", "--verify", "--quiet", "HEAD"])
.filter(|commit| !commit.is_empty())
}
fn holds_own(&self, folder: &Path) -> bool {
if !std::fs::symlink_metadata(folder.join(".git")).is_ok_and(|metadata| metadata.is_dir()) {
return false;
}
let Some(top) = self.run(folder, &["rev-parse", "--show-toplevel"]) else {
return false;
};
let same = |path: &Path| std::fs::canonicalize(path).ok();
same(Path::new(&top)).is_some_and(|top| Some(top) == same(folder))
}
pub fn clone(&self, to: &ToClone, folder: &Path) -> Result<(), String> {
let mut cloning = self.command();
cloning
.arg("clone")
.arg("--no-recurse-submodules")
.arg(format!("--origin={}", to.remote));
if let Some(branch) = &to.branch {
cloning.arg(format!("--branch={branch}"));
}
cloning.arg("--").arg(&to.address).arg(folder);
checked(cloning)?;
for (name, address) in &to.others {
let mut adding = self.command();
adding
.arg("-C")
.arg(folder)
.args(["remote", "add", "--"])
.arg(name)
.arg(address);
checked(adding)?;
}
if let Some(pinned) = &to.pinned {
let mut resetting = self.command();
resetting
.arg("-C")
.arg(folder)
.args(["reset", "--hard", "--quiet"])
.arg(pinned);
checked(resetting)?;
}
Ok(())
}
}
fn checked(mut command: Command) -> Result<(), String> {
let output = command
.env("LC_ALL", "C")
.env_remove("LANGUAGE")
.stdout(Stdio::null())
.output()
.map_err(|error| format!("Git could not run: {error}"))?;
if output.status.success() {
Ok(())
} else {
let said = String::from_utf8_lossy(&output.stderr).trim().to_owned();
Err(if said.is_empty() {
format!("Git failed: {}", output.status)
} else {
said
})
}
}