use std::path::Path;
use amont_agent_guard::git;
use amont_agent_guard::sha256::{hex, sha256};
const PLANS_DIR: &[u8] = b"docs/plans/";
pub fn content_id(repo: &Path, rev: &str) -> Result<String, String> {
let spec = format!("{rev}^{{tree}}");
let entries = git::ls_tree_entries(repo, &spec)
.map_err(|e| format!("git could not be run in {}: {e}", repo.display()))?
.ok_or_else(|| format!("`{rev}` does not name a tree in {}", repo.display()))?;
Ok(content_id_of(&entries))
}
pub fn content_id_of(entries: &[Vec<u8>]) -> String {
let mut bytes: Vec<u8> = Vec::new();
for entry in entries {
let path = entry
.iter()
.position(|c| *c == b'\t')
.map(|i| &entry[i + 1..])
.unwrap_or(&entry[..]);
if path.starts_with(PLANS_DIR) {
continue;
}
bytes.extend_from_slice(entry);
bytes.push(0);
}
hex(&sha256(&bytes))
}