use std::collections::HashSet;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::path::Path;
use std::process::Command;
use std::sync::{Arc, Mutex};
use super::HiveError;
use super::state::HiveStore;
use super::vault::VaultMap;
const RUN_LINES: usize = 3;
const MIN_LINE_CHARS: usize = 8;
const BLOCK_BYTES: usize = 100;
const MIN_BLOCK_INK: usize = 40;
const MAX_FILES: usize = 50_000;
const MAX_FILE_BYTES: u64 = 1024 * 1024;
const MAX_TOTAL_BYTES: u64 = 256 * 1024 * 1024;
pub fn check(store: &HiveStore, fields: &[(&str, &str)]) -> Result<(), HiveError> {
if fields.iter().all(|(_, text)| text.trim().is_empty()) {
return Ok(());
}
let roots: Vec<String> = VaultMap::load(store.data_dir())
.vaults
.into_values()
.flat_map(|project| project.roots)
.collect();
let index = cached_index(&roots)?;
for (name, text) in fields {
if index.holds(text) {
return Err(HiveError::Refused(format!(
"the {name} was not sent: it contains text from your own repositories"
)));
}
}
Ok(())
}
fn unchecked() -> HiveError {
HiveError::Refused(
"not sent: the text could not be checked against your own repositories".into(),
)
}
fn git(root: &Path, args: &[&str]) -> Result<Vec<u8>, HiveError> {
Command::new("git")
.arg("-C")
.arg(root)
.args(args)
.output()
.ok()
.filter(|out| out.status.success())
.map(|out| out.stdout)
.ok_or_else(unchecked)
}
fn fingerprint(roots: &[String]) -> Result<u64, HiveError> {
let mut hasher = DefaultHasher::new();
for root in roots.iter().map(Path::new).filter(|r| r.is_dir()) {
root.hash(&mut hasher);
git(root, &["rev-parse", "-q", "--verify", "HEAD"])
.unwrap_or_default()
.hash(&mut hasher);
let status = git(root, &["status", "--porcelain=v1", "-z", "-uall"])?;
for entry in status.split(|b| *b == 0).filter(|e| e.len() > 3) {
entry.hash(&mut hasher);
if let Ok(meta) =
std::fs::symlink_metadata(root.join(String::from_utf8_lossy(&entry[3..]).as_ref()))
{
meta.len().hash(&mut hasher);
meta.modified().ok().hash(&mut hasher);
}
}
}
Ok(hasher.finish())
}
static CACHE: Mutex<Option<(u64, Arc<Index>)>> = Mutex::new(None);
fn cached_index(roots: &[String]) -> Result<Arc<Index>, HiveError> {
let key = fingerprint(roots)?;
if let Some((cached, index)) = CACHE.lock().ok().and_then(|c| c.clone())
&& cached == key
{
return Ok(index);
}
let index = Arc::new(Index::of_roots(roots.iter().map(Path::new))?);
if let Ok(mut cache) = CACHE.lock() {
*cache = Some((key, index.clone()));
}
Ok(index)
}
fn hash(value: impl Hash) -> u64 {
let mut hasher = DefaultHasher::new();
value.hash(&mut hasher);
hasher.finish()
}
fn significant_lines(text: &str) -> Vec<&str> {
text.lines()
.map(str::trim)
.filter(|line| {
line.chars().count() >= MIN_LINE_CHARS && line.chars().any(char::is_alphanumeric)
})
.collect()
}
fn inked(block: &[u8]) -> bool {
block.iter().filter(|b| !b.is_ascii_whitespace()).count() >= MIN_BLOCK_INK
}
#[derive(Default)]
struct Index {
runs: HashSet<u64>,
blocks: HashSet<u64>,
}
impl Index {
fn of_roots<'p>(roots: impl Iterator<Item = &'p Path>) -> Result<Self, HiveError> {
let mut index = Self::default();
let (mut files, mut total) = (0usize, 0u64);
for root in roots {
if !root.is_dir() {
continue;
}
let listed = git(root, &["ls-files", "-z", "-co", "--exclude-standard"])?;
for name in listed.split(|b| *b == 0).filter(|n| !n.is_empty()) {
if files >= MAX_FILES || total >= MAX_TOTAL_BYTES {
return Ok(index);
}
let path = root.join(String::from_utf8_lossy(name).as_ref());
let Ok(meta) = std::fs::symlink_metadata(&path) else {
continue;
};
if !meta.is_file() || meta.len() > MAX_FILE_BYTES {
continue;
}
let Ok(bytes) = std::fs::read(&path) else {
continue;
};
let Ok(text) = std::str::from_utf8(&bytes) else {
continue;
};
files += 1;
total += meta.len();
index.add(text);
}
}
Ok(index)
}
fn add(&mut self, text: &str) {
for run in significant_lines(text).windows(RUN_LINES) {
self.runs.insert(hash(run));
}
for block in text.as_bytes().as_chunks::<BLOCK_BYTES>().0 {
if inked(block) {
self.blocks.insert(hash(block.as_slice()));
}
}
}
fn holds(&self, text: &str) -> bool {
if significant_lines(text)
.windows(RUN_LINES)
.any(|run| self.runs.contains(&hash(run)))
{
return true;
}
text.as_bytes()
.windows(BLOCK_BYTES)
.any(|window| inked(window) && self.blocks.contains(&hash(window)))
}
}
#[cfg(test)]
mod tests {
use super::*;
const SOURCE: &str = "\
use std::collections::BTreeMap;
/// Settle every open invoice of the quarter.
pub fn reconcile_quarterly_settlements(ledger: &mut BTreeMap<u64, i64>) -> i64 {
let mut balance = 0;
for (invoice, amount) in ledger.iter_mut() {
balance += *amount * i64::try_from(*invoice % 7).unwrap_or(1);
*amount = 0;
}
balance
}
";
fn index() -> Index {
let mut index = Index::default();
index.add(SOURCE);
index
}
#[test]
fn three_copied_lines_are_a_hit_even_reindented() {
let pasted = "Here is how I would do it:\n\
let mut balance = 0;\n\
for (invoice, amount) in ledger.iter_mut() {\n\
balance += *amount * i64::try_from(*invoice % 7).unwrap_or(1);\n";
assert!(index().holds(pasted));
let two = "let mut balance = 0;\nfor (invoice, amount) in ledger.iter_mut() {\n";
assert!(!index().holds(two));
}
#[test]
fn two_hundred_contiguous_bytes_are_a_hit_wherever_they_start() {
for start in [0, 13, 57, 99, 140] {
let run = &SOURCE[start..start + 200];
let flat = format!("see: {}", run.replace('\n', " "));
assert!(!index().holds(&flat), "flattening breaks the bytes too");
assert!(index().holds(&format!("see: {run} end")), "start {start}");
}
}
#[test]
fn own_words_and_common_short_lines_are_no_hit() {
let answer = "The quarter's balance is the sum of each amount weighted by the \
invoice number modulo seven; amounts are reset to zero afterwards.\n\
}\n\
}\n\
balance\n";
assert!(!index().holds(answer));
assert!(!index().holds(&" ".repeat(400)));
}
#[test]
fn a_linked_checkout_is_read_from_its_files_except_ignored_ones() {
let repo = tempfile::tempdir().unwrap();
let git = |args: &[&str]| {
assert!(
Command::new("git")
.arg("-C")
.arg(repo.path())
.args(args)
.output()
.unwrap()
.status
.success()
);
};
git(&["init", "-q"]);
std::fs::write(repo.path().join("ledger.rs"), SOURCE).unwrap();
git(&["add", "ledger.rs"]);
let ignored = "let ignored_sauce_formula = 42;\nlet ignored_sauce_formula = 43;\nlet ignored_sauce_formula = 44;\n";
std::fs::write(repo.path().join(".gitignore"), "local.env\n").unwrap();
std::fs::write(repo.path().join("local.env"), ignored).unwrap();
let roots = vec![
repo.path().to_string_lossy().into_owned(),
repo.path().join("gone").to_string_lossy().into_owned(),
];
let index = cached_index(&roots).unwrap();
assert!(index.holds(&SOURCE[40..260]), "tracked");
assert!(!index.holds(ignored), "ignored files stay out");
let note = "let fresh_sauce_formula = 42;\nlet fresh_sauce_formula = 43;\nlet fresh_sauce_formula = 44;\n";
assert!(!cached_index(&roots).unwrap().holds(note));
std::fs::write(repo.path().join("notes.md"), note).unwrap();
assert!(cached_index(&roots).unwrap().holds(note), "untracked");
let edited = "let edited_sauce_formula = 1;\nlet edited_sauce_formula = 2;\nlet edited_sauce_formula = 3;\n";
std::fs::write(repo.path().join("notes.md"), edited).unwrap();
assert!(cached_index(&roots).unwrap().holds(edited), "edited");
}
#[test]
#[ignore = "measurement"]
fn index_time_of_this_checkout() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
let roots = vec![root.to_string_lossy().into_owned()];
let started = std::time::Instant::now();
let index = Index::of_roots(roots.iter().map(Path::new)).unwrap();
let built = started.elapsed();
let started = std::time::Instant::now();
cached_index(&roots).unwrap();
let first = started.elapsed();
let started = std::time::Instant::now();
cached_index(&roots).unwrap();
let again = started.elapsed();
println!(
"own_text: {} runs, {} blocks; build {built:?}, first check {first:?}, cached check {again:?}",
index.runs.len(),
index.blocks.len()
);
}
}