tidy: factor out change detection logic and make it more robust

now does proper parsing of git's output and falls back to
assuming all files are modified if `git` doesn't work.

accepts a closure so extensions can be checked.
This commit is contained in:
binarycat 2025-07-03 13:37:20 -05:00
parent 66bc234087
commit 9eb180541a
2 changed files with 30 additions and 16 deletions

View file

@ -124,6 +124,32 @@ pub fn git_diff<S: AsRef<OsStr>>(base_commit: &str, extra_arg: S) -> Option<Stri
Some(String::from_utf8_lossy(&output.stdout).into())
}
/// Returns true if any modified file matches the predicate, or if unable to list modified files.
pub fn files_modified(base_commit: &str, pred: impl Fn(&str) -> bool) -> bool {
match crate::git_diff(&base_commit, "--name-status") {
Some(output) => {
let modified_files = output.lines().filter_map(|ln| {
let (status, name) = ln
.trim_end()
.split_once('\t')
.expect("bad format from `git diff --name-status`");
if status == "M" { Some(name) } else { None }
});
for modified_file in modified_files {
if pred(modified_file) {
return true;
}
}
false
}
None => {
eprintln!("warning: failed to run `git diff` to check for changes");
eprintln!("warning: assuming all files are modified");
true
}
}
}
pub mod alphabetical;
pub mod bins;
pub mod debug_artifacts;

View file

@ -14,22 +14,10 @@ pub fn check(src_path: &Path, ci_info: &crate::CiInfo, bad: &mut bool) {
};
// First we check that `src/rustdoc-json-types` was modified.
match crate::git_diff(&base_commit, "--name-status") {
Some(output) => {
if !output
.lines()
.any(|line| line.starts_with("M") && line.contains(RUSTDOC_JSON_TYPES))
{
// `rustdoc-json-types` was not modified so nothing more to check here.
println!("`rustdoc-json-types` was not modified.");
return;
}
}
None => {
*bad = true;
eprintln!("error: failed to run `git diff` in rustdoc_json check");
return;
}
if !crate::files_modified(base_commit, |p| p == RUSTDOC_JSON_TYPES) {
// `rustdoc-json-types` was not modified so nothing more to check here.
println!("`rustdoc-json-types` was not modified.");
return;
}
// Then we check that if `FORMAT_VERSION` was updated, the `Latest feature:` was also updated.
match crate::git_diff(&base_commit, src_path.join("rustdoc-json-types")) {