eazygit 0.5.1

A fast TUI for Git with staging, conflicts, rebase, and palette-first UX
Documentation
//! Branch operations: current_branch, merged_local_branches, delete_local_branch.

use anyhow::{bail, Result};
use std::process::Command;
use super::GitCli;

impl GitCli {
    /// Get the current branch name.
    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())
    }

    /// List merged local branches relative to base.
    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)
    }

    /// Delete a local branch.
    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(())
    }
}