use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use sha2::{Digest, Sha256};
use super::{BaselineError, CodeGraph};
#[derive(Debug, Clone)]
pub struct FileFacts {
pub hash: String,
pub symbols: Vec<String>,
}
pub struct Base {
graph: CodeGraph,
commit: String,
files: HashMap<String, FileFacts>,
}
impl Base {
pub fn build_at(root: &Path, reference: &str) -> Result<Arc<Self>, BaselineError> {
if !crate::git::is_repo(root) {
return Err(BaselineError::NoRepo);
}
let Some(commit) = crate::git::resolve_commit(root, reference) else {
return Err(BaselineError::UnresolvedRef(reference.to_string()));
};
let sources: Vec<(String, String, &'static str)> = crate::git::list_files_at(root, &commit)
.into_iter()
.filter_map(|path| {
let ext = path.extension().and_then(std::ffi::OsStr::to_str)?;
let language = crate::extract::language_for_extension(ext)?;
let source = crate::git::read_blob_at(root, &commit, &path)?;
Some((path.display().to_string(), source, language))
})
.collect();
let mut files: HashMap<String, FileFacts> = sources
.iter()
.map(|(rel, source, _)| {
let hash = format!("{:x}", Sha256::digest(source.as_bytes()));
(
rel.clone(),
FileFacts {
hash,
symbols: Vec::new(),
},
)
})
.collect();
let graph = CodeGraph::from_sources(sources.into_iter());
for ix in graph.graph.node_indices() {
let node = &graph.graph[ix];
if let Some(facts) = files.get_mut(&node.file) {
facts.symbols.push(node.name.clone());
}
}
Ok(Arc::new(Self {
graph,
commit,
files,
}))
}
#[must_use]
pub fn graph(&self) -> &CodeGraph {
&self.graph
}
#[must_use]
pub fn commit(&self) -> &str {
&self.commit
}
#[must_use]
pub fn file(&self, rel: &str) -> Option<&FileFacts> {
self.files.get(rel)
}
#[must_use]
pub fn file_count(&self) -> usize {
self.files.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn committed_repo() -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
let run = |args: &[&str]| {
let out = std::process::Command::new("git")
.arg("-C")
.arg(dir.path())
.args(args)
.output()
.unwrap();
assert!(out.status.success(), "git {args:?} failed");
};
run(&["init", "-q", "-b", "main"]);
run(&["config", "user.email", "t@t"]);
run(&["config", "user.name", "t"]);
std::fs::write(dir.path().join("leaf.rs"), "fn leaf() {}\n").unwrap();
std::fs::write(dir.path().join("mid.rs"), "fn mid() { leaf(); }\n").unwrap();
std::fs::write(dir.path().join("twin.rs"), "fn leaf() {}\n").unwrap();
run(&["add", "-A"]);
run(&["commit", "-qm", "base"]);
dir
}
#[test]
fn base_records_resolved_commit_per_file_hashes_and_symbols() {
let dir = committed_repo();
let base = Base::build_at(dir.path(), "main").unwrap();
assert_eq!(base.commit().len(), 40, "resolved id, not the ref spelling");
assert!(base.graph().has_symbol("leaf"));
let mid = base.file("mid.rs").expect("mid.rs is in the base tree");
assert_eq!(mid.symbols, ["mid"]);
assert_eq!(mid.hash.len(), 64, "hex sha256");
assert!(base.file("absent.rs").is_none());
assert_eq!(base.file_count(), 3);
}
#[test]
fn identical_content_hashes_identically_the_fr15_sharing_key() {
let dir = committed_repo();
let base = Base::build_at(dir.path(), "main").unwrap();
let (leaf, twin) = (base.file("leaf.rs").unwrap(), base.file("twin.rs").unwrap());
assert_eq!(leaf.hash, twin.hash, "same bytes must share one FR-15 key");
assert_ne!(base.file("mid.rs").unwrap().hash, leaf.hash);
}
#[test]
fn the_base_is_shared_not_copied() {
let dir = committed_repo();
let base = Base::build_at(dir.path(), "main").unwrap();
let other = Arc::clone(&base);
assert!(
std::ptr::eq(Arc::as_ptr(&base), Arc::as_ptr(&other)),
"tenants share ONE base allocation"
);
}
#[test]
fn a_base_that_cannot_build_says_so_never_builds_empty() {
let no_repo = tempfile::tempdir().unwrap();
assert_eq!(
Base::build_at(no_repo.path(), "HEAD").err(),
Some(BaselineError::NoRepo)
);
let dir = committed_repo();
assert_eq!(
Base::build_at(dir.path(), "no-such-ref").err(),
Some(BaselineError::UnresolvedRef("no-such-ref".to_string()))
);
}
#[test]
fn the_base_ignores_the_working_tree() {
let dir = committed_repo();
std::fs::write(dir.path().join("late.rs"), "fn late() {}\n").unwrap();
let base = Base::build_at(dir.path(), "main").unwrap();
assert!(
!base.graph().has_symbol("late"),
"the base reads the committed tree, never the working copy"
);
assert!(base.file("late.rs").is_none());
}
}