use crate::error::{Result, ToriiError};
use std::path::Path;
use std::process::Command;
pub fn start(repo_path: &Path, bad: Option<&str>, good: &[String]) -> Result<()> {
let mut args = vec!["bisect".to_string(), "start".to_string()];
if let Some(b) = bad {
args.push(b.to_string());
}
for g in good {
args.push(g.to_string());
}
run_git(repo_path, &args)
}
pub fn bad(repo_path: &Path, commit: Option<&str>) -> Result<()> {
let mut args = vec!["bisect".to_string(), "bad".to_string()];
if let Some(c) = commit {
args.push(c.to_string());
}
run_git(repo_path, &args)
}
pub fn good(repo_path: &Path, commit: Option<&str>) -> Result<()> {
let mut args = vec!["bisect".to_string(), "good".to_string()];
if let Some(c) = commit {
args.push(c.to_string());
}
run_git(repo_path, &args)
}
pub fn skip(repo_path: &Path, commit: Option<&str>) -> Result<()> {
let mut args = vec!["bisect".to_string(), "skip".to_string()];
if let Some(c) = commit {
args.push(c.to_string());
}
run_git(repo_path, &args)
}
pub fn reset(repo_path: &Path) -> Result<()> {
run_git(repo_path, &["bisect".to_string(), "reset".to_string()])
}
pub fn log(repo_path: &Path) -> Result<()> {
run_git(repo_path, &["bisect".to_string(), "log".to_string()])
}
pub fn run(repo_path: &Path, cmd: &[String]) -> Result<()> {
if cmd.is_empty() {
return Err(ToriiError::InvalidConfig(
"bisect run needs a command to execute".into(),
));
}
let mut args = vec!["bisect".to_string(), "run".to_string()];
args.extend(cmd.iter().cloned());
run_git(repo_path, &args)
}
fn run_git(repo_path: &Path, args: &[String]) -> Result<()> {
let status = Command::new("git")
.args(args)
.current_dir(repo_path)
.status()
.map_err(|e| ToriiError::InvalidConfig(format!("invoke git bisect: {e}")))?;
if !status.success() {
return Err(ToriiError::InvalidConfig(format!(
"git {} exited with {}",
args.join(" "),
status
)));
}
Ok(())
}