use std::collections::{BTreeMap, HashMap};
use std::rc::Rc;
use brygge_ir::{
AtomId, BlobId, ChangeAtom, Content, Identity, Ir, PathOp, RefKind, RefRecord, SourceKind,
Text, Time,
};
use super::trailers;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct GitRef {
pub(crate) name: String,
pub(crate) target: GitRefTarget,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum GitRefTarget {
Oid(Vec<u8>),
Symbolic(String),
}
fn diff_source_label(source_kind: &SourceKind, source_atom_id: &[u8]) -> String {
format!(
"source {}",
trailers::truncate_for_refusal(&trailers::source_id_text(source_kind, source_atom_id))
)
}
fn diff_commit_suffix(commit_atom_id: &[u8]) -> String {
format!(
" (commit {})",
trailers::truncate_for_refusal(&trailers::to_hex(commit_atom_id))
)
}
#[derive(Debug)]
pub(crate) enum Comparison {
Corresponds,
DoesNotCorrespond(Vec<String>),
NotChecked(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum EntryState {
Blob(u32, BlobId),
Submodule(Vec<u8>),
}
type TreeState = BTreeMap<String, EntryState>;
fn apply_ops(state: &mut TreeState, ops: &[PathOp]) {
for op in ops {
match op {
PathOp::Add { path, content, .. }
| PathOp::Modify { path, content, .. }
| PathOp::Replace { path, content, .. } => match content {
Content::Blob { blob, mode } => {
state.insert(path.clone(), EntryState::Blob(*mode, *blob));
}
Content::Submodule { commit } => {
state.insert(path.clone(), EntryState::Submodule(commit.clone()));
}
},
PathOp::Delete { path, .. } => {
state.remove(path);
}
}
}
}
fn parent_use_counts(atoms: &[ChangeAtom]) -> HashMap<AtomId, u32> {
let mut counts = HashMap::new();
for atom in atoms {
if let Some(first) = atom.parents.first() {
*counts.entry(*first).or_insert(0) += 1;
}
}
counts
}
struct Side<'a> {
atoms: &'a [ChangeAtom],
by_id: HashMap<AtomId, &'a ChangeAtom>,
next: usize,
ancestor: HashMap<AtomId, Rc<TreeState>>,
ancestor_remaining: HashMap<AtomId, u32>,
pending: HashMap<AtomId, Rc<TreeState>>,
}
impl<'a> Side<'a> {
fn new(atoms: &'a [ChangeAtom]) -> Self {
Self {
atoms,
by_id: atoms.iter().map(|a| (a.id, a)).collect(),
next: 0,
ancestor: HashMap::new(),
ancestor_remaining: parent_use_counts(atoms),
pending: HashMap::new(),
}
}
fn done(&self) -> bool {
self.next >= self.atoms.len()
}
fn produce(&mut self, matched: &HashMap<AtomId, AtomId>) {
let Some(atom) = self.atoms.get(self.next) else {
return;
};
self.next += 1;
let mut state = match atom.parents.first() {
Some(p) => self
.ancestor
.get(p)
.map(|rc| (**rc).clone())
.unwrap_or_default(),
None => TreeState::new(),
};
apply_ops(&mut state, &atom.ops);
let rc = Rc::new(state);
self.ancestor.insert(atom.id, rc.clone());
if matched.contains_key(&atom.id) {
self.pending.insert(atom.id, rc);
}
if let Some(first) = atom.parents.first() {
if let Some(c) = self.ancestor_remaining.get_mut(first) {
*c = c.saturating_sub(1);
if *c == 0 {
self.ancestor.remove(first);
}
}
}
}
}
fn compare_atoms(ir: &Ir, result: &Ir, digest_hex: &str, diffs: &mut Vec<String>) {
let mut artifact_to_result: HashMap<AtomId, AtomId> = HashMap::new();
let mut result_to_artifact: HashMap<AtomId, AtomId> = HashMap::new();
let mut by_source_id: HashMap<Vec<u8>, AtomId> = HashMap::new();
for atom in &ir.atoms {
by_source_id.insert(atom.source.atom_id.clone(), atom.id);
}
let result_by_id: HashMap<AtomId, &ChangeAtom> =
result.atoms.iter().map(|a| (a.id, a)).collect();
for commit in &result.atoms {
let message = commit
.metadata
.message
.as_ref()
.map(|t| t.bytes.as_slice())
.unwrap_or(&[]);
let label = || {
format!(
"commit {}",
trailers::truncate_for_refusal(&trailers::source_id_text(
&SourceKind::Git,
&commit.source.atom_id
))
)
};
let Some(value) = trailers::extract_trailer(message, "Brygge-Source-Id") else {
diffs.push(format!("{}: has no Brygge-Source-Id trailer", label()));
continue;
};
let Some(bytes) = trailers::parse_source_id(&ir.provenance.source.kind, &value) else {
diffs.push(format!(
"{}: its Brygge-Source-Id ({value:?}) does not parse as this source's id",
label()
));
continue;
};
let Some(&artifact_id) = by_source_id.get(&bytes) else {
diffs.push(format!(
"{}: its Brygge-Source-Id names no atom in the artifact",
label()
));
continue;
};
if let Some(existing) = artifact_to_result.insert(artifact_id, commit.id) {
#[allow(clippy::expect_used)]
let existing_atom = result_by_id.get(&existing).expect("result's own atom id");
diffs.push(format!(
"{}: two result commits both claim it, commit {} and commit {}",
diff_source_label(&ir.provenance.source.kind, &bytes),
trailers::truncate_for_refusal(&trailers::to_hex(&existing_atom.source.atom_id)),
trailers::truncate_for_refusal(&trailers::to_hex(&commit.source.atom_id)),
));
continue;
}
result_to_artifact.insert(commit.id, artifact_id);
}
for atom in &ir.atoms {
if !artifact_to_result.contains_key(&atom.id) {
diffs.push(format!(
"{}: no commit in the result names it",
diff_source_label(&ir.provenance.source.kind, &atom.source.atom_id)
));
}
}
let mut a_side = Side::new(&ir.atoms);
let mut r_side = Side::new(&result.atoms);
while !a_side.done() || !r_side.done() {
if !a_side.done() {
a_side.produce(&artifact_to_result);
}
if !r_side.done() {
r_side.produce(&result_to_artifact);
}
let ready: Vec<(AtomId, AtomId)> = a_side
.pending
.keys()
.filter_map(|aid| {
let rid = *artifact_to_result.get(aid)?;
r_side.pending.contains_key(&rid).then_some((*aid, rid))
})
.collect();
for (aid, rid) in ready {
#[allow(clippy::expect_used)]
let a_state = a_side
.pending
.remove(&aid)
.expect("just filtered as present");
#[allow(clippy::expect_used)]
let r_state = r_side
.pending
.remove(&rid)
.expect("just filtered as present");
#[allow(clippy::expect_used)]
let artifact_atom = *a_side.by_id.get(&aid).expect("atom from its own IR");
#[allow(clippy::expect_used)]
let result_atom = *r_side.by_id.get(&rid).expect("atom from its own IR");
compare_matched_pair(
artifact_atom,
result_atom,
&a_state,
&r_state,
&artifact_to_result,
&ir.transforms,
&ir.provenance.source.kind,
digest_hex,
diffs,
);
}
}
}
fn expected_identity(identity: Option<&Identity>, field: &'static str) -> (String, String) {
let mut supplied = trailers::Supplied::default();
let mut altered = trailers::Altered::default();
let line = trailers::identity_line(identity, &mut supplied, &mut altered, field);
let (name, email_part) = line.rsplit_once(" <").unwrap_or((line.as_str(), ">"));
let email = email_part.strip_suffix('>').unwrap_or(email_part);
(name.to_string(), email.to_string())
}
fn expected_when(
time: Option<Time>,
time_field: &'static str,
tz_field: &'static str,
) -> (i64, i16) {
let mut supplied = trailers::Supplied::default();
let text = trailers::when(time, &mut supplied, time_field, tz_field);
let (secs, offset) = text.split_once(' ').unwrap_or((text.as_str(), "+0000"));
let seconds = secs.parse().unwrap_or(0);
let sign: i16 = if offset.starts_with('-') { -1 } else { 1 };
let digits = offset.get(1..).unwrap_or("0000");
let hh: i16 = digits.get(0..2).and_then(|s| s.parse().ok()).unwrap_or(0);
let mm: i16 = digits.get(2..4).and_then(|s| s.parse().ok()).unwrap_or(0);
(seconds, sign * (hh * 60 + mm))
}
fn identity_matches(result: Option<&Identity>, expected: &(String, String)) -> bool {
let (name, email) = match result {
Some(id) => (
id.name.as_utf8().unwrap_or_default().to_string(),
id.email
.as_ref()
.and_then(Text::as_utf8)
.unwrap_or_default()
.to_string(),
),
None => (String::new(), String::new()),
};
name == expected.0 && email == expected.1
}
fn when_matches(result: Option<Time>, expected: (i64, i16)) -> bool {
match result {
Some(t) => t.seconds == expected.0 && t.offset_minutes.unwrap_or(0) == expected.1,
None => expected == (0, 0),
}
}
fn expected_message(
atom: &ChangeAtom,
transforms: &[brygge_ir::TransformRecord],
source_kind: &SourceKind,
digest_hex: &str,
) -> Vec<u8> {
let mut supplied = trailers::Supplied::default();
let mut altered = trailers::Altered::default();
let _ = trailers::identity_line(
atom.metadata.author.as_ref(),
&mut supplied,
&mut altered,
"author",
);
let _ = trailers::when(
atom.metadata.author_time,
&mut supplied,
"author-time",
"author-time-zone",
);
match (&atom.metadata.committer, atom.metadata.commit_time) {
(None, None) => {
supplied.mark("committer");
supplied.mark("commit-time");
}
(committer, commit_time) => {
if committer.is_some() {
let _ = trailers::identity_line(
committer.as_ref(),
&mut supplied,
&mut altered,
"committer",
);
} else {
supplied.mark("committer");
}
if commit_time.is_some() {
let _ = trailers::when(
commit_time,
&mut supplied,
"commit-time",
"commit-time-zone",
);
} else {
supplied.mark("commit-time");
}
}
}
let source_label = trailers::source_kind_label(source_kind);
let source_id = trailers::source_id_text(&atom.source.kind, &atom.source.atom_id);
let mut lines = vec![
format!(
"Brygge-Encoded: brygge {} from {source_label}",
env!("CARGO_PKG_VERSION")
),
format!("Brygge-Source-Id: {source_id}"),
];
if let Some(l) = supplied.trailer() {
lines.push(l);
}
if let Some(l) = altered.trailer() {
lines.push(l);
}
lines.extend(trailers::status_trailers(atom, transforms));
lines.push(format!("Brygge-Artifact: sha256:{digest_hex}"));
let mut body = atom
.metadata
.message
.as_ref()
.map(|t| t.bytes.clone())
.unwrap_or_default();
if !body.ends_with(b"\n") {
body.push(b'\n');
}
body.push(b'\n');
body.extend(lines.join("\n").into_bytes());
body
}
#[allow(clippy::too_many_arguments)]
fn compare_matched_pair(
artifact_atom: &ChangeAtom,
result_atom: &ChangeAtom,
a_state: &TreeState,
r_state: &TreeState,
artifact_to_result: &HashMap<AtomId, AtomId>,
transforms: &[brygge_ir::TransformRecord],
source_kind: &SourceKind,
digest_hex: &str,
diffs: &mut Vec<String>,
) {
let label = format!(
"{}{}",
diff_source_label(&artifact_atom.source.kind, &artifact_atom.source.atom_id),
diff_commit_suffix(&result_atom.source.atom_id)
);
let mapped_parents: Vec<Option<AtomId>> = artifact_atom
.parents
.iter()
.map(|p| artifact_to_result.get(p).copied())
.collect();
let result_parents = &result_atom.parents;
if mapped_parents.len() != result_parents.len()
|| mapped_parents
.iter()
.zip(result_parents)
.any(|(expected, actual)| expected.as_ref() != Some(actual))
{
diffs.push(format!("{label}: parents differ"));
}
if a_state != r_state {
diffs.push(format!("{label}: the resulting tree differs"));
}
let expected_author = expected_identity(artifact_atom.metadata.author.as_ref(), "author");
if !identity_matches(result_atom.metadata.author.as_ref(), &expected_author) {
diffs.push(format!("{label}: author differs"));
}
let expected_author_when = expected_when(
artifact_atom.metadata.author_time,
"author-time",
"author-time-zone",
);
if !when_matches(result_atom.metadata.author_time, expected_author_when) {
diffs.push(format!("{label}: author time differs"));
}
let (expected_committer, expected_commit_when) = match (
&artifact_atom.metadata.committer,
artifact_atom.metadata.commit_time,
) {
(None, None) => (expected_author.clone(), expected_author_when),
(committer, commit_time) => (
if committer.is_some() {
expected_identity(committer.as_ref(), "committer")
} else {
expected_author.clone()
},
if commit_time.is_some() {
expected_when(commit_time, "commit-time", "commit-time-zone")
} else {
expected_author_when
},
),
};
if !identity_matches(result_atom.metadata.committer.as_ref(), &expected_committer) {
diffs.push(format!("{label}: committer differs"));
}
if !when_matches(result_atom.metadata.commit_time, expected_commit_when) {
diffs.push(format!("{label}: commit time differs"));
}
let result_message = result_atom
.metadata
.message
.as_ref()
.map(|t| t.bytes.as_slice())
.unwrap_or(&[]);
let expected = expected_message(artifact_atom, transforms, source_kind, digest_hex);
if result_message != expected.as_slice() {
diffs.push(format!("{label}: message (with its trailers) differs"));
}
}
fn compare_refs(
ir: &Ir,
result: &Ir,
result_refs: &[GitRef],
artifact_to_result: &HashMap<AtomId, AtomId>,
diffs: &mut Vec<String>,
) {
let result_by_name: HashMap<&str, &GitRef> =
result_refs.iter().map(|r| (r.name.as_str(), r)).collect();
let result_annotated: HashMap<&str, &RefRecord> = result
.refs
.iter()
.filter(|rf| rf.kind == RefKind::Tag && rf.annotation.is_some())
.map(|rf| (rf.name.as_str(), rf))
.collect();
let mut expected_names: std::collections::HashSet<String> = std::collections::HashSet::new();
for rf in &ir.refs {
let path = super::git_ref_path(rf);
expected_names.insert(path.clone());
let Some(&found) = result_by_name.get(path.as_str()) else {
diffs.push(format!("ref {path:?}: missing from the result"));
continue;
};
let expected_target = artifact_to_result.get(&rf.target).copied();
let matches_target = match (&found.target, &rf.annotation) {
(GitRefTarget::Oid(_), Some(_)) => true,
(GitRefTarget::Oid(oid), None) => commit_oid_matches(result, expected_target, oid),
(GitRefTarget::Symbolic(_), _) => false,
};
if !matches_target {
diffs.push(format!("ref {path:?}: points at a different commit"));
}
if let (Some(ann), Some(found_rf)) =
(&rf.annotation, result_annotated.get(rf.name.as_str()))
{
let expected_tagger = expected_identity(ann.tagger.as_ref(), "tagger");
#[allow(clippy::expect_used)]
let found_ann = found_rf
.annotation
.as_ref()
.expect("filtered by annotation.is_some()");
if !identity_matches(found_ann.tagger.as_ref(), &expected_tagger) {
diffs.push(format!("tag {path:?}: tagger differs"));
}
let expected_tag_when = expected_when(ann.time, "tag-time", "tag-time-zone");
if !when_matches(found_ann.time, expected_tag_when) {
diffs.push(format!("tag {path:?}: tag time differs"));
}
} else if rf.annotation.is_some() != result_annotated.contains_key(rf.name.as_str()) {
diffs.push(format!("tag {path:?}: annotation presence differs"));
}
}
for found in result_refs {
if found.name == "HEAD" {
continue;
}
if !expected_names.contains(&found.name) {
diffs.push(format!(
"ref {:?}: not named by the artifact (nothing extra)",
found.name
));
}
}
}
fn commit_oid_matches(result: &Ir, expected_target: Option<AtomId>, oid: &[u8]) -> bool {
let Some(expected_target) = expected_target else {
return false;
};
result
.atoms
.iter()
.find(|a| a.id == expected_target)
.is_some_and(|a| a.source.atom_id == oid)
}
fn compare_head(default_branch_ref: &str, result_refs: &[GitRef], diffs: &mut Vec<String>) {
let Some(head) = result_refs.iter().find(|r| r.name == "HEAD") else {
diffs.push("HEAD: not reported by the repository listing".to_string());
return;
};
match &head.target {
GitRefTarget::Symbolic(name) if name == default_branch_ref => {}
GitRefTarget::Symbolic(name) => {
diffs.push(format!("HEAD: names {name:?}, not {default_branch_ref:?}"));
}
GitRefTarget::Oid(_) => diffs.push("HEAD: detached, not symbolic".to_string()),
}
}
pub(crate) fn compare_git_target(
ir: &Ir,
digest: &[u8; 32],
result: &Ir,
result_refs: &[GitRef],
default_branch_ref: &str,
) -> Comparison {
let mut seen: HashMap<&[u8], &ChangeAtom> = HashMap::new();
for atom in &ir.atoms {
if let Some(first) = seen.insert(&atom.source.atom_id, atom) {
return Comparison::NotChecked(format!(
"two atoms in the artifact share one source id: {} and {}",
trailers::truncate_for_refusal(&trailers::source_id_text(
&ir.provenance.source.kind,
&first.source.atom_id
)),
trailers::truncate_for_refusal(&trailers::source_id_text(
&ir.provenance.source.kind,
&atom.source.atom_id
)),
));
}
}
let digest_hex = trailers::to_hex(digest);
let mut diffs = Vec::new();
compare_atoms(ir, result, &digest_hex, &mut diffs);
let mut by_source_id: HashMap<Vec<u8>, AtomId> = HashMap::new();
for atom in &ir.atoms {
by_source_id.insert(atom.source.atom_id.clone(), atom.id);
}
let mut artifact_to_result: HashMap<AtomId, AtomId> = HashMap::new();
for commit in &result.atoms {
let message = commit
.metadata
.message
.as_ref()
.map(|t| t.bytes.as_slice())
.unwrap_or(&[]);
if let Some(value) = trailers::extract_trailer(message, "Brygge-Source-Id") {
if let Some(bytes) = trailers::parse_source_id(&ir.provenance.source.kind, &value) {
if let Some(&artifact_id) = by_source_id.get(&bytes) {
artifact_to_result.insert(artifact_id, commit.id);
}
}
}
}
compare_refs(ir, result, result_refs, &artifact_to_result, &mut diffs);
compare_head(default_branch_ref, result_refs, &mut diffs);
if diffs.is_empty() {
Comparison::Corresponds
} else {
Comparison::DoesNotCorrespond(diffs)
}
}
#[cfg(test)]
mod tests;