use super::super::{Change, Hit, LOCK_INTEGRITY, Level, shown};
fn entry(text: &str) -> Option<(&str, &str, &str)> {
let mut words = text.split_whitespace();
let (module, version, hash) = (words.next()?, words.next()?, words.next()?);
(version.starts_with('v') && hash.contains(':') && words.next().is_none())
.then_some((module, version, hash))
}
pub(super) fn scan(change: &Change<'_>, hits: &mut Vec<Hit>) {
for block in change.blocks {
let removed: Vec<_> = block.removed.iter().filter_map(|t| entry(t)).collect();
for (line, text) in change.added_in(block) {
let Some((module, version, hash)) = entry(text) else {
continue;
};
let changed = removed
.iter()
.any(|(m, v, h)| *m == module && *v == version && *h != hash);
if changed {
hits.push(Hit::new(
LOCK_INTEGRITY,
Level::Deny,
*line,
format!(
"changes the hash of `{} {}` while its version stays: the next build \
accepts other code under the same version where Go's checksum database \
is not consulted",
shown(module),
shown(version)
),
));
}
}
}
}