use anyhow::{bail, Result};
use std::process::Command;
use super::GitCli;
impl GitCli {
pub fn last_commit_info(&self, repo_path: &str) -> Result<crate::app::state::LastCommitInfo> {
let output = Command::new("git")
.args(["-C", repo_path, "log", "-1", "--format=%an%n%ae%n%ai%n%H%n%h%n%s%n%b"])
.output()?;
if !output.status.success() {
bail!("git log failed: {}", String::from_utf8_lossy(&output.stderr));
}
let text = String::from_utf8_lossy(&output.stdout);
let lines: Vec<&str> = text.lines().collect();
if lines.len() < 6 {
bail!("Invalid commit info format");
}
let subject = lines.get(5).unwrap_or(&"").to_string();
let body = if lines.len() > 6 { lines[6..].join("\n").trim().to_string() } else { String::new() };
let message = if body.is_empty() { subject } else { format!("{}\n\n{}", subject, body) };
Ok(crate::app::state::LastCommitInfo {
author_name: lines[0].to_string(),
author_email: lines[1].to_string(),
message,
date: lines[2].to_string(),
hash: lines[3].to_string(),
short_hash: lines[4].to_string(),
})
}
pub fn commit(&self, repo_path: &str, message: &str, amend: bool, author_name: Option<&str>, author_email: Option<&str>) -> Result<()> {
let mut cmd = Command::new("git");
cmd.args(["-C", repo_path, "commit", "-m", message]);
if amend { cmd.arg("--amend"); }
if let (Some(name), Some(email)) = (author_name, author_email) {
if !name.is_empty() && !email.is_empty() {
cmd.args(["--author", &format!("{} <{}>", name, email)]);
}
}
let output = cmd.output()?;
if !output.status.success() {
bail!("git commit failed: {}", String::from_utf8_lossy(&output.stderr));
}
Ok(())
}
}