use thor_core::{find_repo, list_worktrees};
use std::path::PathBuf;
use std::process::Command;
pub struct ExecResult {
pub branch: String,
pub path: PathBuf,
pub exit_code: i32,
pub output: String,
}
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)
}