|
| 1 | +use std::{ |
| 2 | + fmt::Debug, |
| 3 | + fs::{canonicalize, File}, |
| 4 | + io::{BufRead, BufReader}, |
| 5 | + path::{Path, PathBuf}, |
| 6 | +}; |
| 7 | + |
| 8 | +use glob::{MatchOptions, Pattern}; |
| 9 | + |
| 10 | +#[derive(thiserror::Error, Debug)] |
| 11 | +pub enum Error { |
| 12 | + #[error("io error: {0}")] |
| 13 | + Io(#[from] std::io::Error), |
| 14 | + #[error("non utf8 path")] |
| 15 | + NonUtf8Path, |
| 16 | + #[error("glob pattern error: {0}")] |
| 17 | + Pattern(#[from] glob::PatternError), |
| 18 | +} |
| 19 | + |
| 20 | +pub type Result<T> = std::result::Result<T, Error>; |
| 21 | + |
| 22 | +#[derive(Debug)] |
| 23 | +pub struct Rule { |
| 24 | + negate: bool, |
| 25 | + pattern: Pattern, |
| 26 | + _source_line: usize, |
| 27 | +} |
| 28 | + |
| 29 | +impl Rule { |
| 30 | + pub fn parse( |
| 31 | + mut pattern: String, |
| 32 | + base_path: impl AsRef<Path>, |
| 33 | + _source_line: usize, |
| 34 | + ) -> Result<Option<Self>> { |
| 35 | + if pattern.trim().is_empty() || pattern.starts_with('#') { |
| 36 | + return Ok(None); |
| 37 | + } |
| 38 | + let negate = if pattern.starts_with('!') { |
| 39 | + pattern.remove(0); |
| 40 | + true |
| 41 | + } else { |
| 42 | + false |
| 43 | + }; |
| 44 | + let directory = if pattern.ends_with('/') { |
| 45 | + pattern.pop(); |
| 46 | + true |
| 47 | + } else { |
| 48 | + false |
| 49 | + }; |
| 50 | + let anchored = pattern.contains('/'); |
| 51 | + let pattern = if anchored { |
| 52 | + let base = format!("{}/{pattern}", base_path.as_ref().to_str().unwrap()); |
| 53 | + if directory { |
| 54 | + format!("{base}/**") |
| 55 | + } else { |
| 56 | + base |
| 57 | + } |
| 58 | + } else if !pattern.starts_with("**") { |
| 59 | + let base = format!("**/{pattern}"); |
| 60 | + if directory { |
| 61 | + format!("{base}/**") |
| 62 | + } else { |
| 63 | + base |
| 64 | + } |
| 65 | + } else { |
| 66 | + pattern |
| 67 | + }; |
| 68 | + Ok(Some(Self { |
| 69 | + negate, |
| 70 | + pattern: Pattern::new(&pattern)?, |
| 71 | + _source_line, |
| 72 | + })) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[derive(Debug)] |
| 77 | +pub struct Gitignore { |
| 78 | + rules: Vec<Rule>, |
| 79 | + _source_file: PathBuf, |
| 80 | +} |
| 81 | + |
| 82 | +impl Gitignore { |
| 83 | + /// Parses a `.gitignore` file at `path`. |
| 84 | + /// |
| 85 | + /// If `path` is a directory, attempts to read `{dir}/.gitignore`. |
| 86 | + pub fn parse(path: impl AsRef<Path>) -> Result<Self> { |
| 87 | + let mut path = canonicalize(path)?; |
| 88 | + if path.is_dir() { |
| 89 | + path = path.join(".gitignore"); |
| 90 | + } |
| 91 | + let reader = BufReader::new(File::open(&path)?); |
| 92 | + let mut rules = Vec::new(); |
| 93 | + for (line_nb, line) in reader.lines().enumerate() { |
| 94 | + let line = line?; |
| 95 | + if let Some(rule) = Rule::parse(line, path.parent().unwrap(), line_nb + 1)? { |
| 96 | + rules.push(rule); |
| 97 | + } |
| 98 | + } |
| 99 | + Ok(Self { |
| 100 | + rules, |
| 101 | + _source_file: path, |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + pub fn ignored(&self, path: impl AsRef<Path>) -> Result<bool> { |
| 106 | + let path = canonicalize(path)?; |
| 107 | + let match_opts = MatchOptions { |
| 108 | + case_sensitive: true, |
| 109 | + require_literal_separator: true, |
| 110 | + require_literal_leading_dot: false, |
| 111 | + }; |
| 112 | + for rule in &self.rules { |
| 113 | + println!("matching {} to {rule:?}", path.to_str().unwrap()); |
| 114 | + let path_str = path.to_str().ok_or(Error::NonUtf8Path)?; |
| 115 | + let to_match = if path.is_dir() { |
| 116 | + format!("{path_str}/") |
| 117 | + } else { |
| 118 | + path_str.to_owned() |
| 119 | + }; |
| 120 | + if rule.pattern.matches_with(&to_match, match_opts) { |
| 121 | + return Ok(!rule.negate); |
| 122 | + } |
| 123 | + } |
| 124 | + Ok(false) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +#[cfg(test)] |
| 129 | +mod tests { |
| 130 | + use std::sync::Once; |
| 131 | + |
| 132 | + use super::*; |
| 133 | + |
| 134 | + static INIT: Once = Once::new(); |
| 135 | + |
| 136 | + fn create_gitignore(rules: &str, name: &str) -> Gitignore { |
| 137 | + INIT.call_once(|| { |
| 138 | + std::env::set_current_dir(canonicalize("../..").unwrap()).unwrap(); |
| 139 | + }); |
| 140 | + std::fs::write(name, rules).unwrap(); |
| 141 | + let gitignore = Gitignore::parse(name).unwrap(); |
| 142 | + std::fs::remove_file(name).unwrap(); |
| 143 | + gitignore |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn test_regular_pattern() { |
| 148 | + let gitignore = create_gitignore("Cargo.toml", "regular_pattern"); |
| 149 | + assert!(gitignore.ignored("Cargo.toml").unwrap()); |
| 150 | + assert!(!gitignore.ignored("LICENSE").unwrap()); |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn test_glob_pattern() { |
| 155 | + let gitignore = create_gitignore("crates/**/Cargo.toml", "glob_pattern"); |
| 156 | + assert!(gitignore.ignored("crates/gitignore/Cargo.toml").unwrap()); |
| 157 | + assert!(gitignore.ignored("crates/llm-ls/Cargo.toml").unwrap()); |
| 158 | + assert!(gitignore.ignored("crates/lsp-client/Cargo.toml").unwrap()); |
| 159 | + assert!(gitignore.ignored("crates/mock_server/Cargo.toml").unwrap()); |
| 160 | + assert!(gitignore.ignored("crates/testbed/Cargo.toml").unwrap()); |
| 161 | + assert!(!gitignore.ignored("crates/llm-ls/src/main.rs").unwrap()); |
| 162 | + assert!(!gitignore.ignored("crates/lsp-client/src/lib.rs").unwrap()); |
| 163 | + assert!(!gitignore.ignored("crates/testbed/src/main.rs").unwrap()); |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn test_negate_glob_pattern() { |
| 168 | + let gitignore = create_gitignore("!crates/**/Cargo.toml", "negate_glob_pattern"); |
| 169 | + assert!(!gitignore.ignored("crates/gitignore/Cargo.toml").unwrap()); |
| 170 | + assert!(!gitignore.ignored("crates/llm-ls/Cargo.toml").unwrap()); |
| 171 | + assert!(!gitignore.ignored("crates/lsp-client/Cargo.toml").unwrap()); |
| 172 | + assert!(!gitignore.ignored("crates/mock_server/Cargo.toml").unwrap()); |
| 173 | + assert!(!gitignore.ignored("crates/testbed/Cargo.toml").unwrap()); |
| 174 | + assert!(!gitignore.ignored("crates/llm-ls/src/main.rs").unwrap()); |
| 175 | + assert!(!gitignore.ignored("crates/lsp-client/src/lib.rs").unwrap()); |
| 176 | + assert!(!gitignore.ignored("crates/testbed/src/main.rs").unwrap()); |
| 177 | + } |
| 178 | + |
| 179 | + #[test] |
| 180 | + fn test_start_glob_pattern() { |
| 181 | + let gitignore = create_gitignore("**/crates/", "start_glob_pattern"); |
| 182 | + assert!(gitignore.ignored("crates/").unwrap()); |
| 183 | + assert!(gitignore.ignored("crates/llm-ls/Cargo.toml").unwrap()); |
| 184 | + assert!(gitignore |
| 185 | + .ignored("crates/testbed/repositories/simple/src/main.rs") |
| 186 | + .unwrap()); |
| 187 | + assert!(!gitignore.ignored("xtask/").unwrap()); |
| 188 | + assert!(!gitignore.ignored("README.md").unwrap()); |
| 189 | + } |
| 190 | + |
| 191 | + #[test] |
| 192 | + fn test_relative_path() { |
| 193 | + let gitignore = create_gitignore("crates/", "relative_path"); |
| 194 | + assert!(gitignore.ignored("crates/").unwrap()); |
| 195 | + assert!(gitignore.ignored("crates/llm-ls/Cargo.toml").unwrap()); |
| 196 | + assert!(gitignore |
| 197 | + .ignored("crates/testbed/repositories/simple/src/main.rs") |
| 198 | + .unwrap()); |
| 199 | + assert!(!gitignore.ignored("xtask/").unwrap()); |
| 200 | + assert!(!gitignore.ignored("README.md").unwrap()); |
| 201 | + } |
| 202 | + |
| 203 | + #[test] |
| 204 | + fn test_negate_pattern() { |
| 205 | + let gitignore = create_gitignore( |
| 206 | + "!Cargo.toml\n\ |
| 207 | + Cargo.toml", |
| 208 | + "negate_pattern", |
| 209 | + ); |
| 210 | + assert!(!gitignore.ignored("Cargo.toml").unwrap()); |
| 211 | + } |
| 212 | +} |
0 commit comments