thor-wt 0.2.1

Worktree workflow commands for Thor
Documentation
use thor_core::{find_repo, list_worktrees};
use std::path::PathBuf;
use std::process::Command;

/// Result of running a command in one worktree.
pub struct ExecResult {
    pub branch: String,
    pub path: PathBuf,
    pub exit_code: i32,
    pub output: String,
}

/// Run a shell command in every worktree.
///
/// The command is passed to `sh -c` for shell expansion.
/// This is intentional — the user provides the command string
/// directly via `thor wt exec <cmd>`, similar to `git worktree foreach`.
pub async fn exec(command: &str) -> anyhow::Result<Vec<ExecResult>> {
    let repo = find_repo()?;
    let worktrees = list_worktrees(&repo).await?;
    let mut results = Vec::new();

    for wt in &worktrees {
        let output = Command::new("sh")
            .args(["-c", command])
            .current_dir(&wt.path)
            .output()?;

        let combined = format!(
            "{}{}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr),
        );

        results.push(ExecResult {
            branch: wt.display_name(),
            path: wt.path.clone(),
            exit_code: output.status.code().unwrap_or(-1),
            output: combined,
        });
    }

    Ok(results)
}