|
| 1 | +use std::path::{Path, PathBuf}; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use rustc_hash::FxHashSet; |
| 5 | + |
| 6 | +use crate::git::get_added_files; |
| 7 | +use crate::hook::Hook; |
| 8 | + |
| 9 | +pub(crate) async fn check_case_conflict( |
| 10 | + hook: &Hook, |
| 11 | + filenames: &[&Path], |
| 12 | +) -> Result<(i32, Vec<u8>)> { |
| 13 | + let work_dir = hook.work_dir(); |
| 14 | + |
| 15 | + // Get all files in the repo |
| 16 | + let output = tokio::process::Command::new("git") |
| 17 | + .arg("ls-files") |
| 18 | + .current_dir(work_dir) |
| 19 | + .output() |
| 20 | + .await?; |
| 21 | + |
| 22 | + let repo_files: FxHashSet<PathBuf> = String::from_utf8_lossy(&output.stdout) |
| 23 | + .lines() |
| 24 | + .map(PathBuf::from) |
| 25 | + .collect(); |
| 26 | + |
| 27 | + // Add directories for repo files |
| 28 | + let mut repo_files_with_dirs = repo_files.clone(); |
| 29 | + for file in &repo_files { |
| 30 | + repo_files_with_dirs.extend(get_parent_dirs(file)); |
| 31 | + } |
| 32 | + |
| 33 | + // Get relevant files (filenames + added files) |
| 34 | + let added = get_added_files(work_dir).await?; |
| 35 | + let mut relevant_files: FxHashSet<PathBuf> = |
| 36 | + filenames.iter().map(|p| p.to_path_buf()).collect(); |
| 37 | + relevant_files.extend(added); |
| 38 | + |
| 39 | + // Add directories for relevant files |
| 40 | + let mut relevant_files_with_dirs = relevant_files.clone(); |
| 41 | + for file in &relevant_files { |
| 42 | + relevant_files_with_dirs.extend(get_parent_dirs(file)); |
| 43 | + } |
| 44 | + |
| 45 | + // Remove relevant files from repo files (we only check conflicts with existing files) |
| 46 | + for file in &relevant_files_with_dirs { |
| 47 | + repo_files_with_dirs.remove(file); |
| 48 | + } |
| 49 | + |
| 50 | + let mut retv = 0; |
| 51 | + let mut conflicts = FxHashSet::default(); |
| 52 | + |
| 53 | + // Check for conflicts between new files and existing files |
| 54 | + let repo_lower = to_lowercase_set(&repo_files_with_dirs); |
| 55 | + let relevant_lower = to_lowercase_set(&relevant_files_with_dirs); |
| 56 | + conflicts.extend(repo_lower.intersection(&relevant_lower).cloned()); |
| 57 | + |
| 58 | + // Check for conflicts among new files themselves |
| 59 | + let mut lowercase_relevant = to_lowercase_set(&relevant_files_with_dirs); |
| 60 | + for filename in &relevant_files_with_dirs { |
| 61 | + let lower = filename.to_string_lossy().to_lowercase(); |
| 62 | + if lowercase_relevant.contains(&lower) { |
| 63 | + lowercase_relevant.remove(&lower); |
| 64 | + } else { |
| 65 | + conflicts.insert(lower); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + let mut output = Vec::new(); |
| 70 | + if !conflicts.is_empty() { |
| 71 | + let mut conflicting_files: Vec<PathBuf> = repo_files_with_dirs |
| 72 | + .union(&relevant_files_with_dirs) |
| 73 | + .filter(|f| conflicts.contains(&f.to_string_lossy().to_lowercase())) |
| 74 | + .cloned() |
| 75 | + .collect(); |
| 76 | + conflicting_files.sort(); |
| 77 | + |
| 78 | + for filename in conflicting_files { |
| 79 | + let line = format!( |
| 80 | + "Case-insensitivity conflict found: {}\n", |
| 81 | + filename.display() |
| 82 | + ); |
| 83 | + output.extend(line.into_bytes()); |
| 84 | + } |
| 85 | + retv = 1; |
| 86 | + } |
| 87 | + |
| 88 | + Ok((retv, output)) |
| 89 | +} |
| 90 | + |
| 91 | +fn get_parent_dirs(file: &Path) -> Vec<PathBuf> { |
| 92 | + let mut dirs = Vec::new(); |
| 93 | + let mut current = file; |
| 94 | + while let Some(parent) = current.parent() { |
| 95 | + if parent == Path::new("") { |
| 96 | + break; |
| 97 | + } |
| 98 | + dirs.push(parent.to_path_buf()); |
| 99 | + current = parent; |
| 100 | + } |
| 101 | + dirs |
| 102 | +} |
| 103 | + |
| 104 | +fn to_lowercase_set(files: &FxHashSet<PathBuf>) -> FxHashSet<String> { |
| 105 | + files |
| 106 | + .iter() |
| 107 | + .map(|p| p.to_string_lossy().to_lowercase()) |
| 108 | + .collect() |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg(test)] |
| 112 | +mod tests { |
| 113 | + use super::*; |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn test_get_parent_dirs() { |
| 117 | + let parents = get_parent_dirs(Path::new("foo/bar/baz.txt")); |
| 118 | + assert_eq!(parents, vec![Path::new("foo/bar"), Path::new("foo")]); |
| 119 | + |
| 120 | + let parents = get_parent_dirs(Path::new("single.txt")); |
| 121 | + assert!(parents.is_empty()); |
| 122 | + |
| 123 | + let parents = get_parent_dirs(Path::new("a/b/c/d.txt")); |
| 124 | + assert_eq!( |
| 125 | + parents, |
| 126 | + vec![Path::new("a/b/c"), Path::new("a/b"), Path::new("a")] |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_to_lowercase_set() { |
| 132 | + let mut files = FxHashSet::default(); |
| 133 | + files.insert(PathBuf::from("Foo.txt")); |
| 134 | + files.insert(PathBuf::from("BAR.txt")); |
| 135 | + files.insert(PathBuf::from("baz.TXT")); |
| 136 | + |
| 137 | + let lower = to_lowercase_set(&files); |
| 138 | + assert!(lower.contains("foo.txt")); |
| 139 | + assert!(lower.contains("bar.txt")); |
| 140 | + assert!(lower.contains("baz.txt")); |
| 141 | + assert_eq!(lower.len(), 3); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn test_get_parent_dirs_nested() { |
| 146 | + let parents = get_parent_dirs(Path::new("a/b/c/d/e/f.txt")); |
| 147 | + assert_eq!( |
| 148 | + parents, |
| 149 | + vec![ |
| 150 | + Path::new("a/b/c/d/e"), |
| 151 | + Path::new("a/b/c/d"), |
| 152 | + Path::new("a/b/c"), |
| 153 | + Path::new("a/b"), |
| 154 | + Path::new("a") |
| 155 | + ] |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + #[test] |
| 160 | + fn test_get_parent_dirs_no_slash() { |
| 161 | + let parents = get_parent_dirs(Path::new("file.txt")); |
| 162 | + assert!(parents.is_empty()); |
| 163 | + } |
| 164 | + |
| 165 | + #[test] |
| 166 | + fn test_to_lowercase_set_empty() { |
| 167 | + let files: FxHashSet<PathBuf> = FxHashSet::default(); |
| 168 | + let lower = to_lowercase_set(&files); |
| 169 | + assert!(lower.is_empty()); |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + fn test_to_lowercase_set_mixed_case() { |
| 174 | + let mut files = FxHashSet::default(); |
| 175 | + files.insert(PathBuf::from("FooBar.TXT")); |
| 176 | + files.insert(PathBuf::from("FOOBAR.txt")); |
| 177 | + files.insert(PathBuf::from("foobar.Txt")); |
| 178 | + |
| 179 | + let lower = to_lowercase_set(&files); |
| 180 | + // All three should map to the same lowercase version |
| 181 | + assert!(lower.contains("foobar.txt")); |
| 182 | + assert_eq!(lower.len(), 1); |
| 183 | + } |
| 184 | +} |
0 commit comments