use anyhow::{bail, Result};
use std::process::Command;
use super::GitCli;
impl GitCli {
pub fn current_branch(&self, repo_path: &str) -> Result<String> {
let output = Command::new("git")
.args(["-C", repo_path, "rev-parse", "--abbrev-ref", "HEAD"])
.output()?;
if !output.status.success() {
bail!("git rev-parse failed: {}", String::from_utf8_lossy(&output.stderr));
}
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}
pub fn merged_local_branches(&self, repo_path: &str, base: &str) -> Result<Vec<String>> {
let output = Command::new("git")
.args(["-C", repo_path, "branch", "--merged", base])
.output()?;
if !output.status.success() {
bail!("git branch --merged failed: {}", String::from_utf8_lossy(&output.stderr));
}
let current = self.current_branch(repo_path).unwrap_or_default();
let branches: Vec<String> = String::from_utf8_lossy(&output.stdout)
.lines()
.map(|l| l.trim().trim_start_matches("* ").to_string())
.filter(|b| !b.is_empty() && b != base && b != "main" && b != "master" && *b != current)
.collect();
Ok(branches)
}
pub fn delete_local_branch(&self, repo_path: &str, branch: &str) -> Result<()> {
let output = Command::new("git")
.args(["-C", repo_path, "branch", "-d", branch])
.output()?;
if !output.status.success() {
bail!("git branch -d failed: {}", String::from_utf8_lossy(&output.stderr));
}
Ok(())
}
}