thor-wt 0.2.1

Worktree workflow commands for Thor
Documentation
use std::process::Command;

/// Result of a rebase operation.
pub enum RebaseResult {
    Success { onto: String },
    Conflict,
    Failed(String),
}

/// Fetch origin and rebase current branch onto origin/{base}.
pub fn rebase(base: Option<&str>) -> anyhow::Result<RebaseResult> {
    let base = base.unwrap_or("main");
    let remote_ref = format!("origin/{}", base);

    // Fetch
    let fetch = Command::new("git")
        .args(["fetch", "origin", base])
        .status()?;
    if !fetch.success() {
        return Ok(RebaseResult::Failed(format!("Failed to fetch origin/{}", base)));
    }

    // Rebase
    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()))
        }
    }
}