use anyhow::{bail, Context, Result};
use std::path::Path;
pub fn detect_local_default_branch(repo: &Path) -> Result<String> {
let output = super::git_cmd()
.args(["-C"])
.arg(repo)
.args(["rev-parse", "--abbrev-ref", "HEAD"])
.output()
.context("Failed to run `git rev-parse --abbrev-ref HEAD`")?;
if output.status.success() {
let branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !branch.is_empty() && branch != "HEAD" {
return Ok(branch);
}
}
for candidate in ["main", "master", "develop"] {
let output = super::git_cmd()
.args(["-C"])
.arg(repo)
.args(["rev-parse", "--verify", &format!("refs/heads/{candidate}")])
.output()
.context("Failed to run `git rev-parse`")?;
if output.status.success() {
return Ok(candidate.to_string()); }
}
bail!("Could not detect default branch for the local repository"); }
#[must_use]
pub fn branch_exists_local(repo: &Path, branch: &str) -> bool {
super::git_cmd()
.args(["-C"])
.arg(repo)
.args(["rev-parse", "--verify", &format!("refs/heads/{branch}")])
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}