use std::path::Path;
use crate::check::Outcome;
use crate::hooks::common::{fail, hl, ok, repo_root, staged_files};
use crate::manifest::{Manifest, MANIFEST};
use crate::trust::{self, State};
pub fn run(settings: &crate::config::Settings, manifest: &Manifest) -> Outcome {
if !staged_files(&[]).iter().any(|f| f == MANIFEST) {
return Outcome::Inert;
}
let root = repo_root();
run_in(settings, manifest, Path::new(&root))
}
pub fn run_in(settings: &crate::config::Settings, manifest: &Manifest, root: &Path) -> Outcome {
if manifest.externals.is_empty() {
return Outcome::Passed;
}
match trust::state(root) {
State::Trusted | State::NoManifest => {
ok(
settings,
"amont.conf is trusted as staged; its checks gate this commit",
);
Outcome::Passed
}
state @ (State::Untrusted | State::Changed) => {
let names: Vec<&str> = manifest
.externals
.iter()
.map(|e| e.short_name.as_str())
.collect();
fail(&format!(
"this commit changes {MANIFEST}, and the {} check(s) it declares ({}) \
cannot gate it: {}. Review it with {}, accept it, and commit again \
— otherwise this commit goes through with them unrun.",
names.len(),
names.join(", "),
match state {
State::Changed => "it changed since it was trusted",
_ => "it has never been trusted here",
},
hl("amont trust"),
));
Outcome::Failed
}
}
}