use crate::prompt::Error;
use crate::template::GitRunner;
use crate::workspace::SKILLS_DIR;
use std::path::Path;
pub(super) fn drop_config_bodies(
worktree: &Path,
config_commit: &str,
git: &dyn GitRunner,
) -> Result<(), Error> {
let paths: Vec<String> = inherited(worktree)?
.into_iter()
.map(|name| format!("{SKILLS_DIR}/{name}"))
.filter(|path| {
let spec = format!("{config_commit}:{path}");
git.run(worktree, &["cat-file", "-e", &spec]).is_ok()
})
.collect();
if paths.is_empty() {
return Ok(());
}
let mut args: Vec<&str> = vec!["rm", "-r", "-q", "--ignore-unmatch", "--"];
args.extend(paths.iter().map(String::as_str));
git.run(worktree, &args).map_err(|source| Error::Git {
op: "rm the config's skill bodies",
source,
})
}
fn inherited(worktree: &Path) -> Result<Vec<String>, Error> {
let entries = match std::fs::read_dir(worktree.join(SKILLS_DIR)) {
Ok(iter) => iter,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(e) => return Err(e.into()),
};
let mut names: Vec<String> = entries
.flatten()
.map(|e| e.file_name().to_string_lossy().into_owned())
.collect();
names.sort();
Ok(names)
}