use std::io::Write;
use std::path::{Path, PathBuf};
use anyhow::{bail, Context, Result};
use crate::git::{Git, ShellGit};
use crate::state;
use crate::style::{self, col};
pub async fn run(workspace: PathBuf, checkout_original: bool) -> Result<()> {
let c = style::use_color_stdout();
let mut state = match state::load(&workspace)
.with_context(|| format!("fold: loading state in {:?}", workspace))?
{
Some(s) => s,
None => bail!(
"no active run to fold: .pitboss/play/state.json is empty in {:?}",
workspace
),
};
if state.aborted {
let stdout = std::io::stdout();
let mut h = stdout.lock();
let _ = writeln!(
h,
"{} run {} on {} was already folded",
col(c, style::YELLOW, "warning:"),
state.run_id,
state.branch
);
if checkout_original {
checkout_original_branch(&workspace, &state, c).await?;
}
return Ok(());
}
state.aborted = true;
state::save(&workspace, Some(&state))
.with_context(|| format!("fold: persisting state in {:?}", workspace))?;
let stdout = std::io::stdout();
let mut h = stdout.lock();
let _ = writeln!(
h,
"{} run {} on branch {}",
col(c, style::BOLD_RED, "folded"),
state.run_id,
state.branch
);
if checkout_original {
checkout_original_branch(&workspace, &state, c).await?;
}
Ok(())
}
async fn checkout_original_branch(
workspace: &Path,
state: &state::RunState,
c: bool,
) -> Result<()> {
let Some(original) = state.original_branch.as_deref() else {
bail!(
"fold: --checkout-original requested but no original branch is recorded for run {}",
state.run_id
);
};
let git = ShellGit::new(workspace.to_path_buf());
git.checkout(original)
.await
.with_context(|| format!("fold: checking out original branch {:?}", original))?;
let stdout = std::io::stdout();
let mut h = stdout.lock();
let _ = writeln!(
h,
"{} {} {}",
col(c, style::GREEN, "checked out"),
original,
col(c, style::DIM, &format!("(was on {})", state.branch))
);
Ok(())
}