use std::path::{Path, PathBuf};
use super::cmd::{GitError, git, git_ok, git_success};
pub fn workdir(dir: &Path) -> Result<PathBuf, GitError> {
let output = git(dir, &["rev-parse", "--show-toplevel"])?;
Ok(PathBuf::from(output))
}
pub fn stage_files(dir: &Path, paths: &[&str]) -> Result<(), GitError> {
let existing: Vec<&str> = paths
.iter()
.filter(|p| dir.join(p).exists())
.copied()
.collect();
if existing.is_empty() {
return Ok(());
}
let mut args = vec!["add", "--"];
args.extend(existing);
git_ok(dir, &args)
}
pub fn stage_tracked_modified(dir: &Path) -> Result<(), GitError> {
git_ok(dir, &["add", "-u"])
}
pub fn create_commit(dir: &Path, message: &str) -> Result<(), GitError> {
git_ok(dir, &["commit", "-m", message])
}
pub fn create_signed_commit(dir: &Path, message: &str) -> Result<(), GitError> {
git_ok(dir, &["commit", "-S", "-m", message])
}
pub fn create_signed_commit_amend(dir: &Path, message: &str, amend: bool) -> Result<(), GitError> {
if amend {
git_ok(dir, &["commit", "-S", "--amend", "-m", message])
} else {
git_ok(dir, &["commit", "-S", "-m", message])
}
}
pub fn amend_commit(dir: &Path, message: &str) -> Result<(), GitError> {
git_ok(dir, &["commit", "--amend", "-m", message])
}
pub fn create_annotated_tag(dir: &Path, name: &str, message: &str) -> Result<(), GitError> {
git_ok(dir, &["tag", "-a", name, "-m", message])
}
pub fn create_signed_tag(dir: &Path, name: &str, message: &str) -> Result<(), GitError> {
git_ok(dir, &["tag", "-s", "-a", name, "-m", message])
}
pub fn is_working_tree_dirty(dir: &Path) -> Result<bool, GitError> {
let output = git(dir, &["status", "--porcelain"])?;
Ok(!output.is_empty())
}
pub fn create_branch(dir: &Path, name: &str) -> Result<(), GitError> {
git_ok(dir, &["branch", name])
}
pub fn branch_exists(dir: &Path, name: &str) -> Result<bool, GitError> {
let refspec = format!("refs/heads/{name}");
git_success(dir, &["rev-parse", "--verify", &refspec])
}
pub fn checkout_branch(dir: &Path, name: &str) -> Result<(), GitError> {
git_ok(dir, &["checkout", name])
}
pub fn push_follow_tags(dir: &Path, remote: &str) -> Result<(), GitError> {
git_ok(dir, &["push", "--follow-tags", remote, "HEAD"])
}