use std::collections::{BTreeMap, BTreeSet};
use std::path::{Component, Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
use lean_ctx_protocol::cloud::gate::{
ChangedPathV1, FeatureResultV1, G6RecordV1, GATE_RECORD_SCHEMA, GateResult, LockedChange,
LockedPathsV1, MISSING_CI_PIPELINE, PROCESS_VERSION, RULE_LOCKED_PATHS_CHANGED, RerunReportV1,
RerunV1, TaskProcessGateV1, TaskProcessV1, TaskRisk, TestStatusV1, under_locked,
validate_repo_path,
};
use lean_ctx_time::{SecondsFormat, UtcDateTime};
use sha2::{Digest as _, Sha256};
#[path = "gate_list.rs"]
mod list;
#[path = "gate_manifest.rs"]
mod manifest;
#[path = "gate_report.rs"]
mod report;
#[path = "gate_run.rs"]
mod run;
#[path = "gate_tree.rs"]
mod tree;
pub use run::GateDirs;
use run::{
CARGO_CONFIGS, LockedFiles, MoveWatch, Unlock, cargo_run_dir, make_private_dir, private_dir,
run_command, run_env,
};
use tree::{export_tree, private_copy};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GateError {
LockedPathOutsideRepo(String),
Check(String),
}
impl std::fmt::Display for GateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::LockedPathOutsideRepo(path) => write!(
f,
"locked path {path:?} lies outside the repository; the gate refuses it"
),
Self::Check(why) => write!(f, "the G6 check could not run: {why}"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GateInputs {
pub risk: TaskRisk,
pub spec_commit: String,
pub locked_paths: Vec<String>,
pub command: String,
pub e2e_command: Option<String>,
pub features: Vec<String>,
pub timeout: Duration,
}
pub const RERUN_TIMEOUT: Duration = Duration::from_mins(45);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Missing {
Cloud(&'static str),
Local(&'static str),
}
pub fn inputs_for(
process: &TaskProcessV1,
gate: Option<&TaskProcessGateV1>,
start_commit: Option<&str>,
) -> Result<GateInputs, Missing> {
fn given(v: Option<&String>) -> Option<&str> {
v.map(String::as_str)
.map(str::trim)
.filter(|v| !v.is_empty())
}
let none = TaskProcessGateV1::default();
let gate = gate.unwrap_or(&none);
if process.risk.has_spec() {
let spec = given(process.spec_commit.as_ref())
.ok_or(Missing::Cloud("the feature list's spec_commit"))?;
if gate.locked_paths.is_empty() {
return Err(Missing::Cloud("the feature list's locked_paths"));
}
let command = given(gate.test_command.as_ref())
.ok_or(Missing::Cloud("the feature list's test_command"))?;
Ok(GateInputs {
risk: process.risk,
spec_commit: spec.to_owned(),
locked_paths: gate.locked_paths.clone(),
command: command.to_owned(),
e2e_command: given(gate.e2e_command.as_ref()).map(str::to_owned),
features: gate.features.clone(),
timeout: RERUN_TIMEOUT,
})
} else {
let command = given(gate.repo_test_command.as_ref())
.ok_or(Missing::Cloud("the repo's declared test_command (repo-ci)"))?;
let start = start_commit
.map(str::trim)
.filter(|s| !s.is_empty())
.ok_or(Missing::Local("the commit this task's claim started from"))?;
Ok(GateInputs {
risk: process.risk,
spec_commit: start.to_owned(),
locked_paths: Vec::new(),
command: command.to_owned(),
e2e_command: None,
features: Vec::new(),
timeout: RERUN_TIMEOUT,
})
}
}
const GIT_SAFE_CONFIG: [&str; 18] = [
"-c",
"core.fsmonitor=false",
"-c",
"core.hooksPath=/dev/null",
"-c",
"core.quotePath=false",
"-c",
"core.attributesFile=/dev/null",
"-c",
"diff.external=",
"-c",
"diff.ignoreSubmodules=none",
"-c",
"core.untrackedCache=false",
"-c",
"protocol.file.allow=always",
"-c",
"uploadpack.allowAnySHA1InWant=true",
];
fn git(repo: &Path) -> Command {
let mut cmd = Command::new("git");
cmd.env_clear()
.env("PATH", run::system_path())
.env("LANG", "C")
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_NO_REPLACE_OBJECTS", "1")
.env("GIT_TERMINAL_PROMPT", "0")
.arg("--no-replace-objects")
.arg("-C")
.arg(repo)
.args(GIT_SAFE_CONFIG)
.stdin(Stdio::null());
cmd
}
fn run_git(mut cmd: Command, what: &str) -> Result<Vec<u8>, GateError> {
let out = cmd
.output()
.map_err(|e| GateError::Check(format!("git {what}: {e}")))?;
if !out.status.success() {
return Err(GateError::Check(format!(
"git {what} failed: {}",
String::from_utf8_lossy(&out.stderr).trim()
)));
}
Ok(out.stdout)
}
pub fn resolve_commit(repo: &Path, rev: &str) -> Result<String, GateError> {
if rev.starts_with('-') {
return Err(GateError::Check(format!("{rev:?} is not a commit")));
}
let mut cmd = git(repo);
cmd.args(["rev-parse", "--verify", "--quiet", "--end-of-options"])
.arg(format!("{rev}^{{commit}}"));
let out = run_git(cmd, "rev-parse")?;
let sha = String::from_utf8_lossy(&out).trim().to_owned();
if sha.len() == 40 && sha.bytes().all(|b| b.is_ascii_hexdigit()) {
Ok(sha.to_ascii_lowercase())
} else {
Err(GateError::Check(format!("{rev:?} is not a commit")))
}
}
pub fn head_commit(repo: &Path) -> Result<String, GateError> {
resolve_commit(repo, "HEAD")
}
fn nul_fields(bytes: &[u8]) -> impl Iterator<Item = String> + '_ {
bytes
.split(|b| *b == 0)
.filter(|f| !f.is_empty())
.map(|f| String::from_utf8_lossy(f).into_owned())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
File,
Symlink,
Dir,
}
fn tree_entries(repo: &Path, commit: &str) -> Result<Vec<(String, Kind)>, GateError> {
let mut cmd = git(repo);
cmd.args(["ls-tree", "-r", "-t", "-z", "--full-tree", commit]);
let out = run_git(cmd, "ls-tree")?;
let mut entries = Vec::new();
for field in nul_fields(&out) {
let Some((meta, path)) = field.split_once('\t') else {
continue;
};
let kind = match meta.split(' ').next() {
Some("040000" | "160000") => Kind::Dir,
Some("120000") => Kind::Symlink,
_ => Kind::File,
};
entries.push((path.to_owned(), kind));
}
Ok(entries)
}
fn worktree_kind(root: &Path, path: &str) -> Option<Kind> {
let meta = std::fs::symlink_metadata(root.join(path)).ok()?;
let ft = meta.file_type();
Some(if ft.is_symlink() {
Kind::Symlink
} else if ft.is_dir() {
Kind::Dir
} else {
Kind::File
})
}
fn lexical(path: &Path) -> PathBuf {
let mut out = PathBuf::new();
for component in path.components() {
match component {
Component::ParentDir => {
out.pop();
}
Component::CurDir => {}
other => out.push(other),
}
}
out
}
fn resolves_outside(root: &Path, canonical_root: &Path, path: &str) -> bool {
let link = root.join(path);
let Ok(target) = std::fs::read_link(&link) else {
return false;
};
let parent = link.parent().unwrap_or(root);
let joined = lexical(&parent.join(&target));
if !joined.starts_with(lexical(root)) && !joined.starts_with(canonical_root) {
return true;
}
match std::fs::canonicalize(&link) {
Ok(resolved) => !resolved.starts_with(canonical_root),
Err(_) => false,
}
}
fn rank(change: LockedChange) -> u8 {
match change {
LockedChange::OutsideRepo => 3,
LockedChange::TypeChanged => 2,
_ => 1,
}
}
pub fn check_locked_paths(
root: &Path,
spec_commit: &str,
checked: &[String],
) -> Result<LockedPathsV1, GateError> {
for path in checked {
validate_repo_path(path).map_err(|_| GateError::LockedPathOutsideRepo(path.clone()))?;
}
let spec = resolve_commit(root, spec_commit)?;
let mut changed: BTreeMap<String, LockedChange> = BTreeMap::new();
if !checked.is_empty() {
let locked = |path: &str| checked.iter().any(|c| under_locked(path, c));
let mut cmd = git(root);
cmd.args([
"diff",
"--no-ext-diff",
"--no-textconv",
"--no-renames",
"--ignore-submodules=none",
"--name-status",
"-z",
&spec,
"--",
]);
let out = run_git(cmd, "diff")?;
let fields: Vec<String> = nul_fields(&out).collect();
for pair in fields.chunks(2) {
let [status, path] = pair else { continue };
if locked(path) {
note(&mut changed, path.clone(), status_change(status));
}
}
let mut cmd = git(root);
cmd.args(["ls-files", "--others", "--exclude-standard", "-z", "--"]);
for path in nul_fields(&run_git(cmd, "ls-files")?) {
if locked(&path) {
note(&mut changed, path, LockedChange::Untracked);
}
}
let canonical_root = std::fs::canonicalize(root)
.map_err(|e| GateError::Check(format!("the worktree: {e}")))?;
for (path, was) in tree_entries(root, &spec)? {
if !locked(&path) {
continue;
}
let now = worktree_kind(root, &path);
if let Some(now) = now
&& now != was
{
note(&mut changed, path.clone(), LockedChange::TypeChanged);
}
if was == Kind::Symlink
&& now == Some(Kind::Symlink)
&& resolves_outside(root, &canonical_root, &path)
{
note(&mut changed, path, LockedChange::OutsideRepo);
}
}
}
Ok(into_check(spec, checked, changed))
}
pub fn changed_reason(check: &LockedPathsV1) -> String {
let list = check
.changed
.iter()
.map(|c| format!("{} {}", c.path, c.change.as_str()))
.collect::<Vec<_>>()
.join(", ");
let mut reason = format!(
"locked paths differ from spec_commit {}: {list}",
&check.spec_commit[..8.min(check.spec_commit.len())]
);
if reason.chars().count() > 2000 {
reason = reason.chars().take(1997).collect::<String>() + "...";
}
reason
}
fn now_rfc3339() -> String {
UtcDateTime::now().to_rfc3339_opts(SecondsFormat::Secs, true)
}
fn host_name() -> String {
std::fs::read_to_string("/etc/hostname")
.ok()
.map(|h| h.trim().to_owned())
.filter(|h| !h.is_empty() && h.len() <= 128)
.or_else(|| std::env::var("HOSTNAME").ok().filter(|h| !h.is_empty()))
.unwrap_or_else(|| "localhost".to_owned())
}
fn commit_check(
git_dir: &Path,
artifact: &str,
spec: &str,
checked: &[String],
) -> Result<BTreeMap<String, LockedChange>, GateError> {
let locked = |path: &str| checked.iter().any(|c| under_locked(path, c));
let mut changed: BTreeMap<String, LockedChange> = BTreeMap::new();
if checked.is_empty() {
return Ok(changed);
}
let mut cmd = git(git_dir);
cmd.args([
"diff",
"--no-ext-diff",
"--no-textconv",
"--no-renames",
"--ignore-submodules=none",
"--name-status",
"-z",
spec,
artifact,
"--",
]);
let fields: Vec<String> = nul_fields(&run_git(cmd, "diff")?).collect();
for pair in fields.chunks(2) {
let [status, path] = pair else { continue };
if locked(path) {
note(&mut changed, path.clone(), status_change(status));
}
}
let then: BTreeMap<String, Kind> = tree_entries(git_dir, spec)?.into_iter().collect();
for (path, kind) in tree_entries(git_dir, artifact)? {
if locked(&path)
&& let Some(was) = then.get(&path)
&& *was != kind
{
note(&mut changed, path, LockedChange::TypeChanged);
}
}
Ok(changed)
}
fn status_change(status: &str) -> LockedChange {
match status.chars().next() {
Some('A') => LockedChange::Added,
Some('D') => LockedChange::Deleted,
Some('T') => LockedChange::TypeChanged,
_ => LockedChange::Modified,
}
}
fn note(changed: &mut BTreeMap<String, LockedChange>, path: String, change: LockedChange) {
let entry = changed.entry(path).or_insert(change);
if rank(change) > rank(*entry) {
*entry = change;
}
}
fn into_check(
spec: String,
checked: &[String],
changed: BTreeMap<String, LockedChange>,
) -> LockedPathsV1 {
let changed: Vec<ChangedPathV1> = changed
.into_iter()
.map(|(path, change)| ChangedPathV1 { path, change })
.collect();
LockedPathsV1 {
spec_commit: spec,
checked: checked.to_vec(),
ok: changed.is_empty(),
changed,
}
}
fn chooses_the_runner(path: &str) -> bool {
let name = path.rsplit('/').next().unwrap_or(path);
matches!(name, "rust-toolchain" | "rust-toolchain.toml")
|| CARGO_CONFIGS
.iter()
.any(|c| path == *c || path.ends_with(&format!("/{c}")))
}
fn refuse_runner_changes(git_dir: &Path, spec: &str, artifact: &str) -> Result<(), GateError> {
let mut cmd = git(git_dir);
cmd.args([
"diff",
"--no-ext-diff",
"--no-textconv",
"--no-renames",
"--name-only",
"-z",
spec,
artifact,
"--",
]);
let changed: Vec<String> = nul_fields(&run_git(cmd, "diff")?)
.filter(|p| chooses_the_runner(p))
.collect();
if changed.is_empty() {
return Ok(());
}
Err(GateError::Check(format!(
"the judged commit changes {} against spec_commit; cargo's configuration and toolchain choose what runs the tests, so the gate does not run them (a person judges that change)",
changed.join(", ")
)))
}
enum RunVerdict {
Clean,
LockedChanged(BTreeMap<String, LockedChange>),
Failed(String),
}
#[allow(clippy::too_many_arguments)]
fn rerun_in(
git_dir: &Path,
run: &Path,
scratch: &Path,
work: &Path,
commit: &str,
spec: &str,
inputs: &GateInputs,
listed: Option<&list::SpecList>,
) -> Result<(RerunV1, RunVerdict), GateError> {
let tree = run.join("tree");
make_private_dir(&tree)?;
let _unlock = Unlock(run.to_path_buf());
let has_spec = inputs.risk.has_spec();
let locked_paths: &[String] = if has_spec { &inputs.locked_paths } else { &[] };
let locked = |path: &str| locked_paths.iter().any(|c| under_locked(path, c));
export_tree(git_dir, commit, &tree, &|path| !locked(path))?;
if has_spec {
export_tree(git_dir, spec, &tree, &locked)?;
}
LockedFiles::read(&tree, locked_paths)?.lock(&tree)?;
let pinned = LockedFiles::read(&tree, locked_paths)?;
let watch = MoveWatch::new(run)?;
let env = run_env(scratch, work)?;
let target = scratch.join("target");
let log_path = scratch.join("run.log");
let mut log = std::fs::File::create(&log_path)
.map_err(|e| GateError::Check(format!("the run log: {e}")))?;
if let Some(listed) = listed {
use std::io::Write as _;
writeln!(log, "{}", listed.note)
.map_err(|e| GateError::Check(format!("the run log: {e}")))?;
}
let started_at = now_rfc3339();
let deadline = Instant::now() + inputs.timeout;
let mut exit_code = run_command(&tree, &inputs.command, &env, &mut log, deadline)?;
let mut tampered = pinned.changes(
&tree,
&LockedFiles::read(&tree, locked_paths)?,
watch.moved(),
);
if exit_code == 0
&& tampered.is_empty()
&& let Some(e2e) = &inputs.e2e_command
{
exit_code = run_command(&tree, e2e, &env, &mut log, deadline)?;
tampered = pinned.changes(
&tree,
&LockedFiles::read(&tree, locked_paths)?,
watch.moved(),
);
}
let finished_at = now_rfc3339();
drop(log);
let bytes =
std::fs::read(&log_path).map_err(|e| GateError::Check(format!("the run log: {e}")))?;
let log_sha256 = hex::encode(Sha256::digest(&bytes));
let log_text = String::from_utf8_lossy(&bytes);
let (verdict, report) = if !tampered.is_empty() {
(RunVerdict::LockedChanged(tampered), None)
} else if exit_code == 0 && (has_spec || runs_cargo(inputs)) {
judge_cargo_run(
git_dir,
spec,
commit,
&tree,
&target,
locked_paths,
listed.map(|l| &l.tests),
&log_text,
&log_sha256,
)?
} else {
(RunVerdict::Clean, None)
};
let features = if has_spec {
let green = exit_code == 0 && matches!(verdict, RunVerdict::Clean);
inputs
.features
.iter()
.map(|id| FeatureResultV1 {
id: id.clone(),
passes: green,
})
.collect()
} else {
Vec::new()
};
let rerun = RerunV1 {
command: inputs.command.clone(),
e2e_command: inputs.e2e_command.clone(),
host: host_name(),
commit: commit.to_owned(),
spec_commit: spec.to_owned(),
exit_code,
log_sha256,
started_at,
finished_at,
features,
report,
};
Ok((rerun, verdict))
}
fn runs_cargo(inputs: &GateInputs) -> bool {
std::iter::once(&inputs.command)
.chain(inputs.e2e_command.as_ref())
.any(|c| {
c.split(|ch: char| !(ch.is_alphanumeric() || ch == '_' || ch == '-'))
.any(|w| w == "cargo")
})
}
#[allow(clippy::too_many_arguments)]
fn judge_cargo_run(
git_dir: &Path,
spec: &str,
commit: &str,
tree: &Path,
target: &Path,
locked_paths: &[String],
listed: Option<&BTreeMap<String, BTreeSet<String>>>,
log_text: &str,
log_sha256: &str,
) -> Result<(RunVerdict, Option<RerunReportV1>), GateError> {
let build_sources = report::in_tree_build_sources(tree, target);
if let Err(e) = manifest::refuse_changed_sources(git_dir, spec, commit, &build_sources) {
return Ok((RunVerdict::Failed(e.to_string()), None));
}
let rep = report::parse_report(log_text);
if !rep.found {
return Ok((
RunVerdict::Failed(
"rerun_report_missing: the test harness produced no result report (the run ended before the tests ran)".into(),
),
None,
));
}
let mut listed_tests: BTreeMap<String, Vec<String>> = BTreeMap::new();
let mut listed_checked = Vec::new();
let mut not_listed_passed = Vec::new();
for (file, rels) in listed.into_iter().flatten() {
let prefix = match report::module_prefix(tree, file) {
Ok(prefix) => prefix,
Err(e) => {
not_listed_passed.push(format!("{file}: {} tests, but {e}", rels.len()));
continue;
}
};
let one = BTreeMap::from([(
file.clone(),
rels.iter()
.map(|r| format!("{prefix}{r}"))
.collect::<Vec<_>>(),
)]);
let checked = report::locked_statuses(tree, target, &one, &rep);
let failed: Vec<String> = checked
.iter()
.filter(|t| t.status != report::PASSED)
.map(|t| format!("{} ({})", t.test, t.status))
.collect();
if !failed.is_empty() {
not_listed_passed.push(format!("{file}: {}", failed.join(", ")));
}
listed_checked.extend(checked);
listed_tests.extend(one);
}
if !not_listed_passed.is_empty() {
return Ok((
RunVerdict::Failed(format!(
"locked_test_not_run: listed by the compiler at spec_commit: {}",
not_listed_passed.join("; ")
)),
Some(report::record(&rep, log_sha256, listed_checked)),
));
}
let mut tests = match report::locked_tests(tree, locked_paths) {
Ok(tests) => tests,
Err(e) => {
return Ok((
RunVerdict::Failed(format!("locked_test_not_run: {e}")),
Some(report::record(&rep, log_sha256, listed_checked)),
));
}
};
for (file, names) in listed_tests {
let all = tests.entry(file).or_default();
all.extend(names);
all.sort();
all.dedup();
}
let names_checked: Vec<TestStatusV1> = report::locked_statuses(tree, target, &tests, &rep);
let record = report::record(&rep, log_sha256, names_checked.clone());
let fail = |reason: String| Ok((RunVerdict::Failed(reason), Some(record.clone())));
if rep.named_passed > rep.passed {
return fail(format!(
"rerun_report_inconsistent: the harness named {} tests passed but its summaries count {}",
rep.named_passed, rep.passed
));
}
let nameless: Vec<&str> = tests
.iter()
.filter(|(file, names)| names.is_empty() && report::is_test_root(tree, file))
.map(|(file, _)| file.as_str())
.collect();
if !nameless.is_empty() {
return fail(format!(
"locked_test_not_run: {} declares no test the gate can name",
nameless.join(", ")
));
}
let unbuilt = report::unbuilt(tree, target, &tests);
if !unbuilt.is_empty() {
return fail(format!(
"locked_test_not_run: no test binary of the run was built from {}",
unbuilt.join(", ")
));
}
let unmounted = report::unmounted(tree, target, &tests, &rep);
if !unmounted.is_empty() {
return fail(format!(
"locked_test_not_run: the test binary of {} compiled it without mounting it by a plain `mod` chain from its root",
unmounted.join(", ")
));
}
let unfinished = report::unfinished(tree, target, &tests, &rep);
if !unfinished.is_empty() {
return fail(format!(
"locked_test_not_run: the test binary of {} ended before its summary",
unfinished.join(", ")
));
}
let not_passed: Vec<String> = names_checked
.iter()
.filter(|t| t.status != report::PASSED)
.map(|t| format!("{} ({})", t.test, t.status))
.collect();
if !not_passed.is_empty() {
return fail(format!("locked_test_not_run: {}", not_passed.join(", ")));
}
if rep.passed + rep.failed + rep.ignored == 0 {
return fail("rerun_no_tests: the harness reported no test at all".into());
}
Ok((RunVerdict::Clean, Some(record)))
}
fn awaiting_ci_reason(artifact: &str) -> String {
format!(
"locked paths unchanged and the gate's rerun green at {}; the Instance does not read CI, so LeanCTX Cloud completes this record with ci (§5.1)",
&artifact[..12]
)
}
pub fn g6_record(
root: &Path,
dirs: &GateDirs,
artifact: &str,
inputs: &GateInputs,
) -> Result<G6RecordV1, GateError> {
let artifact = resolve_commit(root, artifact)?;
let head = head_commit(root)?;
if head != artifact {
return Err(GateError::Check(format!(
"the worktree is at {}, not at the judged commit {}",
&head[..12],
&artifact[..12]
)));
}
let checked: &[String] = if inputs.risk.has_spec() {
&inputs.locked_paths
} else {
&[]
};
if inputs.risk.has_spec() && checked.is_empty() {
return Err(GateError::Check(
"an M or H task has locked paths; none were given".into(),
));
}
let worktree = check_locked_paths(root, &inputs.spec_commit, checked)?;
let spec = worktree.spec_commit.clone();
let scratch = private_dir(&dirs.work)?;
let git_dir = private_copy(root, scratch.path(), &[&artifact, &spec])?;
let mut changed = commit_check(&git_dir, &artifact, &spec, checked)?;
for item in worktree.changed {
note(&mut changed, item.path, item.change);
}
let base = |result, missing: Vec<String>, locked_paths, rerun, rule: Option<&str>, reason| {
G6RecordV1 {
schema: GATE_RECORD_SCHEMA.into(),
process: PROCESS_VERSION.into(),
gate: "G6".into(),
risk: inputs.risk,
artifact_commit: artifact.clone(),
result,
missing,
locked_paths,
rerun,
rule: rule.map(str::to_owned),
reason,
}
};
let locked_paths = into_check(spec.clone(), checked, changed);
let checked_record = |record: G6RecordV1| {
record
.validate()
.map_err(|e| GateError::Check(format!("the record breaks the contract: {e}")))?;
Ok(record)
};
if !locked_paths.ok {
let reason = changed_reason(&locked_paths);
return checked_record(base(
GateResult::Fail,
Vec::new(),
locked_paths,
None,
Some(RULE_LOCKED_PATHS_CHANGED),
Some(reason),
));
}
let record = {
refuse_runner_changes(&git_dir, &spec, &artifact)?;
manifest::refuse_build_changes(&git_dir, &spec, &artifact)?;
let listed = if inputs.risk.has_spec() && runs_cargo(inputs) {
Some(list::spec_list(
&git_dir,
&spec,
&dirs.runs,
scratch.path(),
&dirs.work,
&inputs.command,
checked,
inputs.timeout,
)?)
} else {
None
};
let run_dir = cargo_run_dir(&dirs.runs)?;
let (run, verdict) = rerun_in(
&git_dir,
run_dir.path(),
scratch.path(),
&dirs.work,
&artifact,
&spec,
inputs,
listed.as_ref(),
)?;
match verdict {
RunVerdict::LockedChanged(tampered) => {
let after = into_check(spec.clone(), checked, tampered);
let reason = format!(
"the gate's run changed locked paths (a build script or test rewrote them): {}",
changed_reason(&after)
);
base(
GateResult::Fail,
Vec::new(),
after,
Some(run),
Some(RULE_LOCKED_PATHS_CHANGED),
Some(reason.chars().take(2000).collect()),
)
}
RunVerdict::Failed(reason) => base(
GateResult::Fail,
Vec::new(),
locked_paths,
Some(run),
None,
Some(reason.chars().take(2000).collect()),
),
RunVerdict::Clean if run.exit_code == 0 => base(
GateResult::Blocked,
vec![MISSING_CI_PIPELINE.to_owned()],
locked_paths,
Some(run),
None,
Some(awaiting_ci_reason(&artifact)),
),
RunVerdict::Clean => {
let reason = format!(
"the gate's run of {:?} at {} exited {}",
run.command,
&artifact[..12],
run.exit_code
);
base(
GateResult::Fail,
Vec::new(),
locked_paths,
Some(run),
None,
Some(reason),
)
}
}
};
checked_record(record)
}
pub fn recheck_commit(
repo: &Path,
work: &Path,
artifact: &str,
spec_commit: &str,
checked: &[String],
) -> Result<LockedPathsV1, GateError> {
for path in checked {
validate_repo_path(path).map_err(|_| GateError::LockedPathOutsideRepo(path.clone()))?;
}
let artifact = resolve_commit(repo, artifact)?;
let spec = resolve_commit(repo, spec_commit)?;
let scratch = private_dir(work)?;
let git_dir = private_copy(repo, scratch.path(), &[&artifact, &spec])?;
let changed = commit_check(&git_dir, &artifact, &spec, checked)?;
Ok(into_check(spec, checked, changed))
}
#[cfg(test)]
#[path = "gate_tests.rs"]
pub(super) mod tests;