use std::io::{Result, Error, ErrorKind}; use std::process::Command; use std::path::Path; pub fn git_save_previous_branch(path: &Path) -> Result { let output = Command::new("git") .arg("rev-parse") .arg("--abbrev-ref") .arg("HEAD") .current_dir(path) .output()?; if !output.status.success() { Err(Error::new(ErrorKind::Other, "Failed to get current branch")) } else { Ok(String::from_utf8(output.stdout).unwrap().trim().to_string()) } } pub fn git_checkout(path: &Path, branch: &str) -> Result<()> { let output = Command::new("git") .arg("checkout") .arg(branch) .current_dir(path) .output()?; if !output.status.success() { Err(Error::new(ErrorKind::Other, format!("Failed to checkout {}", branch))) } else { Ok(()) } } pub fn git_fetch(path: &Path) -> Result<()> { let output = Command::new("git") .arg("fetch") .current_dir(path) .output()?; if !output.status.success() { return Err(Error::new(ErrorKind::Other, "Failed to fetch")); } Ok(()) } pub fn git_check_for_conflicts(path: &Path) -> Result { let merge_output = Command::new("git") .arg("merge") .arg("--no-commit") .arg("--no-ff") .arg("origin/main") .current_dir(path) .output()?; if !merge_output.status.success() { return Err(Error::new(ErrorKind::Other, "Failed to merge")); } let conflicts_output = Command::new("git") .arg("ls-files") .arg("--unmerged") .current_dir(path) .output()?; Command::new("git") .arg("merge") .arg("--abort") .current_dir(path) .output()?; Ok(!conflicts_output.stdout.is_empty()) } pub fn git_merge(path: &Path) -> Result<()> { let output = Command::new("git") .arg("merge") .arg("origin/main") .current_dir(path) .output()?; if !output.status.success() { Err(Error::new(ErrorKind::Other, "Failed to merge")) } else { Ok(()) } } pub fn git_push(path: &Path) -> Result<()> { let output = Command::new("git") .arg("push") .arg("origin") .arg("main") .current_dir(path) .output()?; if !output.status.success() { Err(Error::new(ErrorKind::Other, "Failed to push to origin")) } else { Ok(()) } } pub fn get_commit(path: &Path, revision: &str) -> Result { let output = Command::new("git") .arg("rev-parse") .arg("--short") .arg(revision) .current_dir(path) .output()?; if !output.status.success() { Err(Error::new(ErrorKind::Other, "Failed to get current commit")) } else { Ok(String::from_utf8(output.stdout).unwrap().trim().to_string()) } }