forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilenames.rs
More file actions
40 lines (37 loc) · 1.47 KB
/
filenames.rs
File metadata and controls
40 lines (37 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//! Tidy check to ensure that there are no filenames containing forbidden characters
//! checked into the source tree by accident:
//! - Non-UTF8 filenames
//! - Control characters such as CR or TAB
//! - Filenames containing ":" as they are not supported on Windows
//!
//! Only files added to git are checked, as it may be acceptable to have temporary
//! invalid filenames in the local directory during development.
use std::path::Path;
use std::process::Command;
use crate::diagnostics::TidyCtx;
pub fn check(root_path: &Path, tidy_ctx: TidyCtx) {
let mut check = tidy_ctx.start_check("filenames");
let stat_output = Command::new("git")
.arg("-C")
.arg(root_path)
.args(["ls-files", "-z"])
.output()
.unwrap()
.stdout;
for filename in stat_output.split(|&b| b == 0) {
match str::from_utf8(filename) {
Err(_) => check.error(format!(
r#"non-UTF8 file names are not supported: "{}""#,
String::from_utf8_lossy(filename),
)),
Ok(name) if name.chars().any(|c| c.is_control()) => check.error(format!(
r#"control characters are not supported in file names: "{}""#,
String::from_utf8_lossy(filename),
)),
Ok(name) if name.contains(':') => check.error(format!(
r#"":" is not supported in file names because of Windows compatibility: "{name}""#,
)),
_ => (),
}
}
}