use colored::Colorize;
use weave_core::git::find_repo_root;
use weave_crdt::{reconstruct_file_from_crdt, EntityStateDoc};
pub(crate) fn run(files: &[String]) -> Result<(), Box<dyn std::error::Error>> {
if files.is_empty() {
return Err(
"Specify one or more files to apply, e.g. `weave experimental apply src/lib.rs`".into(),
);
}
let repo_root = find_repo_root(std::path::Path::new("."))?;
let state_path = repo_root.join(".weave").join("state.automerge");
let state = EntityStateDoc::open(&state_path)?;
let mut applied = 0usize;
for file in files {
let reconstructed = reconstruct_file_from_crdt(&state, file)?;
if reconstructed.is_empty() {
println!(
" {} {} — no entities tracked in the CRDT, skipping",
"!".yellow().bold(),
file
);
continue;
}
let target = repo_root.join(file);
let before = std::fs::read_to_string(&target).unwrap_or_default();
if before == reconstructed {
println!(" {} {} — already up to date", "·".dimmed(), file);
continue;
}
std::fs::write(&target, &reconstructed)?;
applied += 1;
println!(" {} {} — materialized from CRDT", "✓".green().bold(), file);
}
println!(
"{} {} file(s) written from the CRDT.",
"weave apply:".bold(),
applied
);
Ok(())
}