use oakum::detect::ReleaseTool;
use super::git::{Git, Op};
use super::repository;
use super::CliError;
const BUMPY_DIR: &str = ".bumpy";
#[derive(Debug, PartialEq, Eq)]
pub(super) enum GateLook {
Found(Vec<String>),
NothingTracked,
Failed(String),
NothingToRepoint,
}
impl GateLook {
pub(super) fn failure(&self) -> Option<&str> {
match self {
Self::Failed(why) => Some(why),
Self::Found(_) | Self::NothingTracked | Self::NothingToRepoint => None,
}
}
}
pub(super) fn find_bump_file_gates(
repo: &repository::Repository,
detections: &[oakum::detect::Detection],
) -> GateLook {
if !detections
.iter()
.any(|hit| hit.tool() == ReleaseTool::Bumpy)
{
return GateLook::NothingToRepoint;
}
Git::at_repository(repo)
.map_err(CliError::from_boxed)
.and_then(|git| gate_look(&git))
.unwrap_or_else(|err| GateLook::Failed(err.detail()))
}
fn gate_look(git: &Git) -> Result<GateLook, CliError> {
if let Some(paths) = git.matched_paths(Op::FilesMentioning { dir: BUMPY_DIR })? {
return Ok(GateLook::Found(paths));
}
if !git.paths(Op::TrackedFiles)?.is_empty() {
return Ok(GateLook::Found(Vec::new()));
}
if git.predicate(Op::RefExists { reference: "HEAD" })? {
return Err(CliError::new(
"git's index lists no file while HEAD has commits, so the index is missing or unbuilt and a gate among the committed files was not searched",
));
}
Ok(GateLook::NothingTracked)
}
#[cfg(test)]
mod tests {
use super::super::git::{Git, Reply};
use super::{gate_look, GateLook};
const GREP: &str = "grep --name-only";
const TRACKED: &str = "ls-files";
const HEAD: &str = "rev-parse --verify";
#[test]
fn a_tracked_file_naming_the_directory_is_found() {
let git = Git::answering([(
GREP,
Reply::said(".claude/hooks/require-bump-file.sh\0.github/workflows/gate.yml\0"),
)]);
assert_eq!(
gate_look(&git).expect("looked"),
GateLook::Found(Vec::from([
String::from(".claude/hooks/require-bump-file.sh"),
String::from(".github/workflows/gate.yml"),
]))
);
assert_eq!(git.asked(), [GREP]);
}
#[test]
fn no_match_over_tracked_files_is_an_empty_find() {
let git = Git::answering([
(GREP, Reply::absent()),
(TRACKED, Reply::said("README.md\0")),
]);
assert_eq!(
gate_look(&git).expect("looked"),
GateLook::Found(Vec::new())
);
assert_eq!(git.asked(), [GREP, TRACKED]);
}
#[test]
fn an_empty_index_with_no_commit_is_nothing_tracked() {
let git = Git::answering([
(HEAD, Reply::absent()),
(TRACKED, Reply::said("")),
(GREP, Reply::absent()),
]);
assert_eq!(gate_look(&git).expect("looked"), GateLook::NothingTracked);
assert_eq!(git.asked(), [GREP, TRACKED, HEAD]);
}
#[test]
fn a_later_child_that_died_or_only_warned_stops_the_look() {
let warned = || Reply::warned("warning: unable to access '/etc/gitconfig'");
for (op, reply) in [
(TRACKED, Reply::was_signalled()),
(TRACKED, warned()),
(HEAD, Reply::was_signalled()),
(HEAD, warned()),
] {
let mut script = Vec::from([(GREP, Reply::absent())]);
if op == HEAD {
script.push((TRACKED, Reply::said("")));
}
let expected: Vec<&str> = script.iter().map(|(op, _)| *op).chain([op]).collect();
script.push((op, reply));
let git = Git::answering(script);
gate_look(&git).expect_err("a child that did not answer stops the look");
assert_eq!(git.asked(), expected, "no child after the one that failed");
}
}
#[test]
fn an_empty_index_over_commits_is_a_failure_to_look() {
let git = Git::answering([
(GREP, Reply::absent()),
(TRACKED, Reply::said("")),
(HEAD, Reply::said("cafe")),
]);
let err = gate_look(&git).expect_err("a broken index is not an empty repository");
assert!(
err.detail()
.contains("index lists no file while HEAD has commits"),
"{err}"
);
}
#[test]
fn a_diagnosed_exit_one_is_a_failure_not_an_empty_search() {
let git = Git::answering([
(GREP, Reply::failed(1, "fatal: unable to read index")),
(TRACKED, Reply::said("README.md\0")),
]);
let err = gate_look(&git).expect_err("a diagnosed failure");
assert!(err.detail().contains("unable to read index"), "{err}");
assert_eq!(git.asked(), [GREP], "no second child after a failed look");
}
}