use std::process::Command;
pub enum RebaseResult {
Success { onto: String },
Conflict,
Failed(String),
}
pub fn rebase(base: Option<&str>) -> anyhow::Result<RebaseResult> {
let base = base.unwrap_or("main");
let remote_ref = format!("origin/{}", base);
let fetch = Command::new("git")
.args(["fetch", "origin", base])
.status()?;
if !fetch.success() {
return Ok(RebaseResult::Failed(format!("Failed to fetch origin/{}", base)));
}
let output = Command::new("git")
.args(["rebase", &remote_ref])
.output()?;
if output.status.success() {
Ok(RebaseResult::Success { onto: remote_ref })
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
if stderr.contains("CONFLICT") || stderr.contains("conflict") {
Ok(RebaseResult::Conflict)
} else {
Ok(RebaseResult::Failed(stderr.trim().to_string()))
}
}
}