Skip to content

Commit 5131130

Browse files
Rollup merge of #143957 - samueltardieu:tidy-filenames, r=Kobzol
tidy: check for invalid file names Check for file names added to git with: - non-UTF8 filenames (this would fail "fmt check" with a decoding error for the moment, but maybe we should not count on it as it is an accidental failure) - control characters (such as "\n" or "\r" in file names) - ":" (which is a special character on Windows, made #142936 fail in bors while it could have be caught earlier) It only checks files known by git as a developer might want to have "strange" file names alongside their local repository as long as they don't check them in. r? jieyouxu as he stumbled upon such a file in #142936
2 parents e8a7109 + cbaaf15 commit 5131130

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/tools/tidy/src/filenames.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! Tidy check to ensure that there are no filenames containing forbidden characters
2+
//! checked into the source tree by accident:
3+
//! - Non-UTF8 filenames
4+
//! - Control characters such as CR or TAB
5+
//! - Filenames containing ":" as they are not supported on Windows
6+
//!
7+
//! Only files added to git are checked, as it may be acceptable to have temporary
8+
//! invalid filenames in the local directory during development.
9+
10+
use std::path::Path;
11+
use std::process::Command;
12+
13+
pub fn check(root_path: &Path, bad: &mut bool) {
14+
let stat_output = Command::new("git")
15+
.arg("-C")
16+
.arg(root_path)
17+
.args(["ls-files", "-z"])
18+
.output()
19+
.unwrap()
20+
.stdout;
21+
for filename in stat_output.split(|&b| b == 0) {
22+
match str::from_utf8(filename) {
23+
Err(_) => tidy_error!(
24+
bad,
25+
r#"non-UTF8 file names are not supported: "{}""#,
26+
String::from_utf8_lossy(filename),
27+
),
28+
Ok(name) if name.chars().any(|c| c.is_control()) => tidy_error!(
29+
bad,
30+
r#"control characters are not supported in file names: "{}""#,
31+
String::from_utf8_lossy(filename),
32+
),
33+
Ok(name) if name.contains(':') => tidy_error!(
34+
bad,
35+
r#"":" is not supported in file names because of Windows compatibility: "{name}""#,
36+
),
37+
_ => (),
38+
}
39+
}
40+
}

src/tools/tidy/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ pub mod error_codes;
167167
pub mod ext_tool_checks;
168168
pub mod extdeps;
169169
pub mod features;
170+
pub mod filenames;
170171
pub mod fluent_alphabetical;
171172
pub mod fluent_period;
172173
mod fluent_used;

src/tools/tidy/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ fn main() {
155155

156156
check!(triagebot, &root_path);
157157

158+
check!(filenames, &root_path);
159+
158160
let collected = {
159161
drain_handles(&mut handles);
160162

0 commit comments

Comments
 (0)