Skip to content

Commit 512cab0

Browse files
committed
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.
1 parent 8c32e87 commit 512cab0

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

src/tools/tidy/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,32 @@ pub fn git_diff<S: AsRef<OsStr>>(base_commit: &str, extra_arg: S) -> Option<Stri
124124
Some(String::from_utf8_lossy(&output.stdout).into())
125125
}
126126

127+
/// Returns true if any modified file matches the predicate, or if unable to list modified files.
128+
pub fn files_modified(base_commit: &str, pred: impl Fn(&str) -> bool) -> bool {
129+
match crate::git_diff(&base_commit, "--name-status") {
130+
Some(output) => {
131+
let modified_files = output.lines().filter_map(|ln| {
132+
let (status, name) = ln
133+
.trim_end()
134+
.split_once('\t')
135+
.expect("bad format from `git diff --name-status`");
136+
if status == "M" { Some(name) } else { None }
137+
});
138+
for modified_file in modified_files {
139+
if pred(modified_file) {
140+
return true;
141+
}
142+
}
143+
false
144+
}
145+
None => {
146+
eprintln!("warning: failed to run `git diff` to check for changes");
147+
eprintln!("warning: assuming all files are modified");
148+
true
149+
}
150+
}
151+
}
152+
127153
pub mod alphabetical;
128154
pub mod bins;
129155
pub mod debug_artifacts;

src/tools/tidy/src/rustdoc_json.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,9 @@ pub fn check(src_path: &Path, ci_info: &crate::CiInfo, bad: &mut bool) {
1414
};
1515

1616
// First we check that `src/rustdoc-json-types` was modified.
17-
match crate::git_diff(&base_commit, "--name-status") {
18-
Some(output) => {
19-
if !output
20-
.lines()
21-
.any(|line| line.starts_with("M") && line.contains(RUSTDOC_JSON_TYPES))
22-
{
23-
// `rustdoc-json-types` was not modified so nothing more to check here.
24-
println!("`rustdoc-json-types` was not modified.");
25-
return;
26-
}
27-
}
28-
None => {
29-
*bad = true;
30-
eprintln!("error: failed to run `git diff` in rustdoc_json check");
31-
return;
32-
}
17+
if crate::files_modified(base_commit, |p| p == RUSTDOC_JSON_TYPES) {
18+
// `rustdoc-json-types` was not modified so nothing more to check here.
19+
println!("`rustdoc-json-types` was not modified.");
3320
}
3421
// Then we check that if `FORMAT_VERSION` was updated, the `Latest feature:` was also updated.
3522
match crate::git_diff(&base_commit, src_path.join("rustdoc-json-types")) {

0 commit comments

Comments
 (0)