use std::process::Command;
pub fn detect_git_ref() -> Option<String> {
let branch = get_current_branch();
let sha = get_head_sha();
match (branch, sha) {
(Some(b), Some(s)) => Some(format!("{} ({})", b, &s[..8.min(s.len())])),
(Some(b), None) => Some(b),
(None, Some(s)) => Some(s[..8.min(s.len())].to_string()),
(None, None) => None,
}
}
fn get_current_branch() -> Option<String> {
let output = Command::new("git")
.args(["rev-parse", "--abbrev-ref", "HEAD"])
.output()
.ok()?;
if output.status.success() {
let branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
if branch == "HEAD" {
None } else {
Some(branch)
}
} else {
None
}
}
fn get_head_sha() -> Option<String> {
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.ok()?;
if output.status.success() {
Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
} else {
None
}
}
pub fn is_git_repo() -> bool {
Command::new("git")
.args(["rev-parse", "--git-dir"])
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}