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::names::old;
use crate::trust::{self, State};
pub fn run(settings: &crate::config::Settings, manifest: &Manifest) -> Outcome {
if !staged_files(&[])
.iter()
.any(|f| f == MANIFEST || f == old::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,
&format!(
"{} is trusted as staged; its checks gate this commit",
manifest.file_name()
),
);
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 {}, 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.",
manifest.file_name(),
names.len(),
names.join(", "),
match state {
State::Changed => "it changed since it was trusted",
_ => "it has never been trusted here",
},
hl(concat!(crate::bin!(), " trust")),
));
Outcome::Failed
}
}
}