Skip to main content

thor_wt/
rebase.rs

1use std::process::Command;
2
3/// Result of a rebase operation.
4pub enum RebaseResult {
5    Success { onto: String },
6    Conflict,
7    Failed(String),
8}
9
10/// Fetch origin and rebase current branch onto origin/{base}.
11pub fn rebase(base: Option<&str>) -> anyhow::Result<RebaseResult> {
12    let base = base.unwrap_or("main");
13    let remote_ref = format!("origin/{}", base);
14
15    // Fetch
16    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    // Rebase
24    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}