Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/edda

crates/nornir-build-thing/src/lfs.rs

//! **A medium never packs a Git LFS pointer** (Holger D13 d7584718, 2026-10-20; shared
//! by lane G10 so every product's compose has it — Gunnar's media had no such check).
//!
//! MEASURED 2026-10-20: test3.holger.rs served `holger.webp` as a 131-byte pointer —
//! `version https://git-lfs.github.com/spec/v1 … size 578646`. The media checkout had
//! never run `git lfs pull`, and holger-front's `include_bytes!` compiled the pointer
//! text in as the picture. Nothing refused: the build was green and the page was broken.
//!
//! [`lfs_pointers`] walks the trees a medium packs (the product checkout and every
//! sibling its rows mount), skips what a medium never packs from ([`NEVER_PACKED`] plus
//! the caller's own list), and names every file whose first bytes are
//! [`LFS_POINTER`]. [`refuse_lfs_pointers`] is the refusal, by name, before a compose.

use std::path::{Path, PathBuf};

use anyhow::{bail, Result};

/// The first line of every Git LFS pointer file.
pub const LFS_POINTER: &[u8] = b"version https://git-lfs.github.com/spec/v1";

/// Directories a medium never packs from: build output, git's own, the composed media,
/// docs, the build declaration's own folder, node's packages.
pub const NEVER_PACKED: [&str; 6] = ["target", ".git", ".iso-out", "docs", ".nornir", "node_modules"];

/// **Every Git LFS pointer in what a medium packs**: each of `roots` walked (a root that
/// does not exist is nothing to pack), every directory named in [`NEVER_PACKED`] or in
/// `skip` passed over, symbolic links not followed. A file that starts with
/// [`LFS_POINTER`] is named.
pub fn lfs_pointers(roots: &[PathBuf], skip: &[&str]) -> Vec<PathBuf> {
    fn walk(dir: &Path, skip: &[&str], out: &mut Vec<PathBuf>) {
        let Ok(entries) = std::fs::read_dir(dir) else { return };
        for e in entries.flatten() {
            let (path, Ok(kind)) = (e.path(), e.file_type()) else { continue };
            if kind.is_dir() {
                let name = e.file_name();
                if !NEVER_PACKED.iter().chain(skip.iter()).any(|n| name == **n) {
                    walk(&path, skip, out);
                }
            } else if kind.is_file() {
                let mut head = [0u8; LFS_POINTER.len()];
                let read = std::fs::File::open(&path).and_then(|mut f| std::io::Read::read_exact(&mut f, &mut head));
                if read.is_ok() && head == LFS_POINTER {
                    out.push(path);
                }
            }
        }
    }
    let mut out = Vec::new();
    for r in roots {
        walk(r, skip, &mut out);
    }
    out
}

/// [`lfs_pointers`] refused by name: `Err` names every pointer and the repair.
pub fn refuse_lfs_pointers(roots: &[PathBuf], skip: &[&str]) -> Result<()> {
    let found = lfs_pointers(roots, skip);
    if !found.is_empty() {
        bail!(
            "REFUSED [medium-lfs-pointer] {} file(s) a medium packs are Git LFS pointers, not their content — a medium would ship the pointer text (run `git lfs install --local && git lfs pull` in each checkout): {}",
            found.len(),
            found.iter().map(|p| p.display().to_string()).collect::<Vec<_>>().join(", ")
        );
    }
    Ok(())
}