thor-wt 0.2.1

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

/// Result of a clean operation.
pub struct CleanResult {
    pub pruned: bool,
    pub deleted_branches: Vec<String>,
}

/// Prune worktrees and optionally delete branches merged into main.
pub fn clean(delete_merged: bool) -> anyhow::Result<CleanResult> {
    // Prune stale worktree references
    let prune = Command::new("git")
        .args(["worktree", "prune"])
        .status()?;
    let pruned = prune.success();

    let mut deleted_branches = Vec::new();

    if delete_merged {
        // Get branches merged into main
        let output = Command::new("git")
            .args(["branch", "--merged", "main"])
            .output()?;

        if output.status.success() {
            let stdout = String::from_utf8_lossy(&output.stdout);
            for line in stdout.lines() {
                let branch = line.trim().trim_start_matches("* ");
                // Skip main, master, and current branch
                if branch == "main" || branch == "master" || branch.is_empty() || line.trim().starts_with('*') {
                    continue;
                }

                let del = Command::new("git")
                    .args(["branch", "-d", branch])
                    .status();

                if del.map(|s| s.success()).unwrap_or(false) {
                    deleted_branches.push(branch.to_string());
                }
            }
        }
    }

    Ok(CleanResult { pruned, deleted_branches })
}