|
| 1 | +use std::ffi::OsStr; |
| 2 | +use std::fs; |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +const EXTENSIONS: &[&str] = |
| 7 | + &["rs", "py", "js", "sh", "c", "cpp", "h", "md", "css", "ftl", "toml", "yml", "yaml"]; |
| 8 | + |
| 9 | +fn has_supported_extension(path: &Path) -> bool { |
| 10 | + path.extension().is_some_and(|ext| EXTENSIONS.iter().any(|e| ext == OsStr::new(e))) |
| 11 | +} |
| 12 | + |
| 13 | +fn list_tracked_files() -> Result<Vec<PathBuf>, String> { |
| 14 | + let output = Command::new("git") |
| 15 | + .args(["ls-files", "-z"]) |
| 16 | + .output() |
| 17 | + .map_err(|e| format!("Failed to run `git ls-files`: {e}"))?; |
| 18 | + |
| 19 | + if !output.status.success() { |
| 20 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 21 | + return Err(format!("`git ls-files` failed: {stderr}")); |
| 22 | + } |
| 23 | + |
| 24 | + let mut files = Vec::new(); |
| 25 | + for entry in output.stdout.split(|b| *b == 0) { |
| 26 | + if entry.is_empty() { |
| 27 | + continue; |
| 28 | + } |
| 29 | + let path = std::str::from_utf8(entry).unwrap(); |
| 30 | + files.push(PathBuf::from(path)); |
| 31 | + } |
| 32 | + |
| 33 | + Ok(files) |
| 34 | +} |
| 35 | + |
| 36 | +pub(crate) fn run() -> Result<(), String> { |
| 37 | + let files = list_tracked_files()?; |
| 38 | + let mut error_count = 0; |
| 39 | + // Avoid embedding the task marker in source so greps only find real occurrences. |
| 40 | + let todo_marker = "todo".to_ascii_uppercase(); |
| 41 | + |
| 42 | + for file in files { |
| 43 | + if !has_supported_extension(&file) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + let bytes = fs::read(&file).unwrap(); |
| 48 | + let contents = std::str::from_utf8(&bytes).unwrap(); |
| 49 | + |
| 50 | + for (i, line) in contents.split('\n').enumerate() { |
| 51 | + let trimmed = line.trim(); |
| 52 | + if trimmed.contains(&todo_marker) { |
| 53 | + eprintln!( |
| 54 | + "{}:{}: {} is used for tasks that should be done before merging a PR; if you want to leave a message in the codebase use FIXME", |
| 55 | + file.display(), |
| 56 | + i + 1, |
| 57 | + todo_marker |
| 58 | + ); |
| 59 | + error_count += 1; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if error_count == 0 { |
| 65 | + return Ok(()); |
| 66 | + } |
| 67 | + |
| 68 | + Err(format!("found {} {}(s)", error_count, todo_marker)) |
| 69 | +} |
0 commit comments