use anyhow::Result;
use crate::cli::RemoveArgs;
use crate::context::Context;
use crate::hooks::{self, Event};
use crate::worktree;
pub fn run(ctx: &Context, args: &RemoveArgs) -> Result<()> {
let repo = ctx.repo()?;
let repo_config = ctx.repo_config_optional();
let target = worktree::require(&repo, &args.name)?;
if args.dry_run {
if ctx.json {
let out = serde_json::json!({
"name": args.name,
"path": target.path.display().to_string(),
"dry_run": true,
});
println!("{}", serde_json::to_string_pretty(&out)?);
} else if !ctx.quiet {
eprintln!(
"would remove worktree: {} at {}",
args.name,
target.path.display()
);
}
return Ok(());
}
let cwd = repo_config
.as_ref()
.map_or_else(|| repo.clone(), |r| r.root.clone());
let event = Event {
cwd: &cwd,
worktree_path: &target.path,
worktree_name: &args.name,
};
let hooks_cfg = repo_config
.as_ref()
.map_or_else(Default::default, |r| r.hooks.clone());
hooks::run_required(hooks_cfg.pre_remove.as_deref(), "pre_remove", &event)?;
worktree::remove(&repo, &args.name, args.force, ctx.quiet)?;
hooks::run_best_effort(hooks_cfg.post_remove.as_deref(), "post_remove", &event);
if ctx.json {
let out = serde_json::json!({
"name": args.name,
"path": target.path.display().to_string(),
});
println!("{}", serde_json::to_string_pretty(&out)?);
} else if !ctx.quiet {
let ok = crate::style::OK;
anstream::eprintln!("{ok}✓{ok:#} removed: {}", args.name);
}
Ok(())
}