1use std::process::Command;
2
3pub enum RebaseResult {
5 Success { onto: String },
6 Conflict,
7 Failed(String),
8}
9
10pub fn rebase(base: Option<&str>) -> anyhow::Result<RebaseResult> {
12 let base = base.unwrap_or("main");
13 let remote_ref = format!("origin/{}", base);
14
15 let fetch = Command::new("git")
17 .args(["fetch", "origin", base])
18 .status()?;
19 if !fetch.success() {
20 return Ok(RebaseResult::Failed(format!("Failed to fetch origin/{}", base)));
21 }
22
23 let output = Command::new("git")
25 .args(["rebase", &remote_ref])
26 .output()?;
27
28 if output.status.success() {
29 Ok(RebaseResult::Success { onto: remote_ref })
30 } else {
31 let stderr = String::from_utf8_lossy(&output.stderr);
32 if stderr.contains("CONFLICT") || stderr.contains("conflict") {
33 Ok(RebaseResult::Conflict)
34 } else {
35 Ok(RebaseResult::Failed(stderr.trim().to_string()))
36 }
37 }
38}