1#![allow(missing_docs)]
16
17use std::fs;
18use std::io;
19use std::iter;
20use std::path::Path;
21use std::path::PathBuf;
22use std::sync::Arc;
23
24use ignore::gitignore;
25use thiserror::Error;
26
27#[derive(Debug, Error)]
28pub enum GitIgnoreError {
29 #[error("Failed to read ignore patterns from file {path}")]
30 ReadFile { path: PathBuf, source: io::Error },
31 #[error("Invalid UTF-8 for ignore pattern in {path} on line #{line_num_for_display}: {line}")]
32 InvalidUtf8 {
33 path: PathBuf,
34 line_num_for_display: usize,
35 line: String,
36 source: std::str::Utf8Error,
37 },
38 #[error("Failed to parse ignore patterns from file {path}")]
39 Underlying {
40 path: PathBuf,
41 source: ignore::Error,
42 },
43}
44
45#[derive(Debug)]
47pub struct GitIgnoreFile {
48 parent: Option<Arc<GitIgnoreFile>>,
49 matcher: gitignore::Gitignore,
50}
51
52impl GitIgnoreFile {
53 pub fn empty() -> Arc<Self> {
54 Arc::new(Self {
55 parent: None,
56 matcher: gitignore::Gitignore::empty(),
57 })
58 }
59
60 pub fn chain(
65 self: &Arc<Self>,
66 prefix: &str,
67 ignore_path: &Path,
68 input: &[u8],
69 ) -> Result<Arc<Self>, GitIgnoreError> {
70 let mut builder = gitignore::GitignoreBuilder::new(prefix);
71 for (i, input_line) in input.split(|b| *b == b'\n').enumerate() {
72 let line =
73 std::str::from_utf8(input_line).map_err(|err| GitIgnoreError::InvalidUtf8 {
74 path: ignore_path.to_path_buf(),
75 line_num_for_display: i + 1,
76 line: String::from_utf8_lossy(input_line).to_string(),
77 source: err,
78 })?;
79 builder
83 .add_line(None, line)
84 .map_err(|err| GitIgnoreError::Underlying {
85 path: ignore_path.to_path_buf(),
86 source: err,
87 })?;
88 }
89 let matcher = builder.build().map_err(|err| GitIgnoreError::Underlying {
90 path: ignore_path.to_path_buf(),
91 source: err,
92 })?;
93 let parent = if self.matcher.is_empty() {
94 self.parent.clone() } else {
96 Some(self.clone())
97 };
98 Ok(Arc::new(Self { parent, matcher }))
99 }
100
101 pub fn chain_with_file(
106 self: &Arc<Self>,
107 prefix: &str,
108 file: PathBuf,
109 ) -> Result<Arc<Self>, GitIgnoreError> {
110 if file.is_file() {
111 let buf = fs::read(&file).map_err(|err| GitIgnoreError::ReadFile {
112 path: file.clone(),
113 source: err,
114 })?;
115 self.chain(prefix, &file, &buf)
116 } else {
117 Ok(self.clone())
118 }
119 }
120
121 fn matches_helper(&self, path: &str, is_dir: bool) -> bool {
122 iter::successors(Some(self), |file| file.parent.as_deref())
123 .find_map(|file| {
124 match file.matcher.matched_path_or_any_parents(path, is_dir) {
128 ignore::Match::None => None,
129 ignore::Match::Ignore(_) => Some(true),
130 ignore::Match::Whitelist(_) => Some(false),
131 }
132 })
133 .unwrap_or_default()
134 }
135
136 pub fn matches(&self, path: &str) -> bool {
144 let (path, is_dir) = match path.strip_suffix('/') {
146 Some(path) => (path, true),
147 None => (path, false),
148 };
149 self.matches_helper(path, is_dir)
150 }
151}
152
153#[cfg(test)]
154mod tests {
155
156 use super::*;
157
158 fn matches(input: &[u8], path: &str) -> bool {
159 let file = GitIgnoreFile::empty()
160 .chain("", Path::new(""), input)
161 .unwrap();
162 file.matches(path)
163 }
164
165 #[test]
166 fn test_gitignore_empty_file() {
167 let file = GitIgnoreFile::empty();
168 assert!(!file.matches("foo"));
169 }
170
171 #[test]
172 fn test_gitignore_empty_file_with_prefix() {
173 let file = GitIgnoreFile::empty()
174 .chain("dir/", Path::new(""), b"")
175 .unwrap();
176 assert!(!file.matches("dir/foo"));
177 }
178
179 #[test]
180 fn test_gitignore_literal() {
181 let file = GitIgnoreFile::empty()
182 .chain("", Path::new(""), b"foo\n")
183 .unwrap();
184 assert!(file.matches("foo"));
185 assert!(file.matches("dir/foo"));
186 assert!(file.matches("dir/subdir/foo"));
187 assert!(!file.matches("food"));
188 assert!(!file.matches("dir/food"));
189 }
190
191 #[test]
192 fn test_gitignore_literal_with_prefix() {
193 let file = GitIgnoreFile::empty()
194 .chain("./dir/", Path::new(""), b"foo\n")
195 .unwrap();
196 assert!(file.matches("dir/foo"));
197 assert!(file.matches("dir/subdir/foo"));
198 }
199
200 #[test]
201 fn test_gitignore_pattern_same_as_prefix() {
202 let file = GitIgnoreFile::empty()
203 .chain("dir/", Path::new(""), b"dir\n")
204 .unwrap();
205 assert!(file.matches("dir/dir"));
206 assert!(!file.matches("dir/foo"));
208 }
209
210 #[test]
211 fn test_gitignore_rooted_literal() {
212 let file = GitIgnoreFile::empty()
213 .chain("", Path::new(""), b"/foo\n")
214 .unwrap();
215 assert!(file.matches("foo"));
216 assert!(!file.matches("dir/foo"));
217 }
218
219 #[test]
220 fn test_gitignore_rooted_literal_with_prefix() {
221 let file = GitIgnoreFile::empty()
222 .chain("dir/", Path::new(""), b"/foo\n")
223 .unwrap();
224 assert!(file.matches("dir/foo"));
225 assert!(!file.matches("dir/subdir/foo"));
226 }
227
228 #[test]
229 fn test_gitignore_deep_dir() {
230 let file = GitIgnoreFile::empty()
231 .chain("", Path::new(""), b"/dir1/dir2/dir3\n")
232 .unwrap();
233 assert!(!file.matches("foo"));
234 assert!(!file.matches("dir1/foo"));
235 assert!(!file.matches("dir1/dir2/foo"));
236 assert!(file.matches("dir1/dir2/dir3/foo"));
237 assert!(file.matches("dir1/dir2/dir3/dir4/foo"));
238 }
239
240 #[test]
241 fn test_gitignore_deep_dir_chained() {
242 let file = GitIgnoreFile::empty()
244 .chain("", Path::new(""), b"/dummy\n")
245 .unwrap()
246 .chain("dir1/", Path::new(""), b"/dummy\n")
247 .unwrap()
248 .chain("dir1/dir2/", Path::new(""), b"/dir3\n")
249 .unwrap();
250 assert!(!file.matches("foo"));
251 assert!(!file.matches("dir1/foo"));
252 assert!(!file.matches("dir1/dir2/foo"));
253 assert!(file.matches("dir1/dir2/dir3/foo"));
254 assert!(file.matches("dir1/dir2/dir3/dir4/foo"));
255 }
256
257 #[test]
258 fn test_gitignore_match_only_dir() {
259 let file = GitIgnoreFile::empty()
260 .chain("", Path::new(""), b"/dir/\n")
261 .unwrap();
262 assert!(!file.matches("dir"));
263 assert!(file.matches("dir/foo"));
264 assert!(file.matches("dir/subdir/foo"));
265 }
266
267 #[test]
268 fn test_gitignore_unusual_symbols() {
269 assert!(matches(b"\\*\n", "*"));
270 assert!(!matches(b"\\*\n", "foo"));
271 assert!(matches(b"\\!\n", "!"));
272 assert!(matches(b"\\?\n", "?"));
273 assert!(!matches(b"\\?\n", "x"));
274 assert!(matches(b"\\w\n", "w"));
275 assert!(
276 GitIgnoreFile::empty()
277 .chain("", Path::new(""), b"\\\n")
278 .is_err()
279 );
280 }
281
282 #[test]
283 #[cfg(not(target_os = "windows"))]
284 fn test_gitignore_backslash_path() {
285 assert!(!matches(b"/foo/bar", "/foo\\bar"));
286 assert!(!matches(b"/foo/bar", "/foo/bar\\"));
287
288 assert!(!matches(b"/foo/bar/", "/foo\\bar/"));
289 assert!(!matches(b"/foo/bar/", "/foo\\bar\\/"));
290
291 assert!(!matches(b"\\w\n", "\\w"));
293 assert!(matches(b"\\\\ \n", "\\ "));
294 assert!(matches(b"\\\\\\ \n", "\\ "));
295 }
296
297 #[test]
298 #[cfg(target_os = "windows")]
299 fn test_gitignore_backslash_path() {
302 assert!(matches(b"/foo/bar", "/foo\\bar"));
303 assert!(matches(b"/foo/bar", "/foo/bar\\"));
304
305 assert!(matches(b"/foo/bar/", "/foo\\bar/"));
306 assert!(matches(b"/foo/bar/", "/foo\\bar\\/"));
307
308 assert!(matches(b"\\w\n", "\\w"));
309 assert!(!matches(b"\\\\ \n", "\\ "));
310 assert!(!matches(b"\\\\\\ \n", "\\ "));
311 }
312
313 #[test]
314 fn test_gitignore_whitespace() {
315 assert!(!matches(b" \n", " "));
316 assert!(matches(b"\\ \n", " "));
317 assert!(!matches(b"\\\\ \n", " "));
318 assert!(matches(b" a\n", " a"));
319 assert!(matches(b"a b\n", "a b"));
320 assert!(matches(b"a b \n", "a b"));
321 assert!(!matches(b"a b \n", "a b "));
322 assert!(matches(b"a b\\ \\ \n", "a b "));
323 assert!(matches(b"a\r\n", "a"));
325 assert!(!matches(b"a\r\n", "a\r"));
326 assert!(!matches(b"a\r\r\n", "a\r"));
327 assert!(matches(b"a\r\r\n", "a"));
328 assert!(!matches(b"a\r\r\n", "a\r\r"));
329 assert!(matches(b"a\r\r\n", "a"));
330 assert!(matches(b"\ra\n", "\ra"));
331 assert!(!matches(b"\ra\n", "a"));
332 assert!(
333 GitIgnoreFile::empty()
334 .chain("", Path::new(""), b"a b \\ \n")
335 .is_err()
336 );
337 }
338
339 #[test]
340 fn test_gitignore_glob() {
341 assert!(!matches(b"*.o\n", "foo"));
342 assert!(matches(b"*.o\n", "foo.o"));
343 assert!(!matches(b"foo.?\n", "foo"));
344 assert!(!matches(b"foo.?\n", "foo."));
345 assert!(matches(b"foo.?\n", "foo.o"));
346 }
347
348 #[test]
349 fn test_gitignore_range() {
350 assert!(!matches(b"foo.[az]\n", "foo"));
351 assert!(matches(b"foo.[az]\n", "foo.a"));
352 assert!(!matches(b"foo.[az]\n", "foo.g"));
353 assert!(matches(b"foo.[az]\n", "foo.z"));
354 assert!(!matches(b"foo.[a-z]\n", "foo"));
355 assert!(matches(b"foo.[a-z]\n", "foo.a"));
356 assert!(matches(b"foo.[a-z]\n", "foo.g"));
357 assert!(matches(b"foo.[a-z]\n", "foo.z"));
358 assert!(matches(b"foo.[0-9a-fA-F]\n", "foo.5"));
359 assert!(matches(b"foo.[0-9a-fA-F]\n", "foo.c"));
360 assert!(matches(b"foo.[0-9a-fA-F]\n", "foo.E"));
361 assert!(!matches(b"foo.[0-9a-fA-F]\n", "foo._"));
362 }
363
364 #[test]
365 fn test_gitignore_leading_dir_glob() {
366 assert!(matches(b"**/foo\n", "foo"));
367 assert!(matches(b"**/foo\n", "dir1/dir2/foo"));
368 assert!(matches(b"**/foo\n", "foo/file"));
369 assert!(matches(b"**/dir/foo\n", "dir/foo"));
370 assert!(matches(b"**/dir/foo\n", "dir1/dir2/dir/foo"));
371 }
372
373 #[test]
374 fn test_gitignore_leading_dir_glob_with_prefix() {
375 let file = GitIgnoreFile::empty()
376 .chain("dir1/dir2/", Path::new(""), b"**/foo\n")
377 .unwrap();
378 assert!(file.matches("dir1/dir2/foo"));
379 assert!(!file.matches("dir1/dir2/bar"));
380 assert!(file.matches("dir1/dir2/sub1/sub2/foo"));
381 assert!(!file.matches("dir1/dir2/sub1/sub2/bar"));
382 }
383
384 #[test]
385 fn test_gitignore_trailing_dir_glob() {
386 assert!(!matches(b"abc/**\n", "abc"));
387 assert!(matches(b"abc/**\n", "abc/file"));
388 assert!(matches(b"abc/**\n", "abc/dir/file"));
389 }
390
391 #[test]
392 fn test_gitignore_internal_dir_glob() {
393 assert!(matches(b"a/**/b\n", "a/b"));
394 assert!(matches(b"a/**/b\n", "a/x/b"));
395 assert!(matches(b"a/**/b\n", "a/x/y/b"));
396 assert!(!matches(b"a/**/b\n", "ax/y/b"));
397 assert!(!matches(b"a/**/b\n", "a/x/yb"));
398 assert!(!matches(b"a/**/b\n", "ab"));
399 }
400
401 #[test]
402 fn test_gitignore_internal_dir_glob_not_really() {
403 assert!(!matches(b"a/x**y/b\n", "a/b"));
404 assert!(matches(b"a/x**y/b\n", "a/xy/b"));
405 assert!(matches(b"a/x**y/b\n", "a/xzzzy/b"));
406 }
407
408 #[test]
409 fn test_gitignore_line_ordering() {
410 assert!(matches(b"foo\n!foo/bar\n", "foo"));
411 assert!(!matches(b"foo\n!foo/bar\n", "foo/bar"));
412 assert!(matches(b"foo\n!foo/bar\n", "foo/baz"));
413 assert!(matches(b"foo\n!foo/bar\nfoo/bar/baz", "foo"));
414 assert!(!matches(b"foo\n!foo/bar\nfoo/bar/baz", "foo/bar"));
415 assert!(matches(b"foo\n!foo/bar\nfoo/bar/baz", "foo/bar/baz"));
416 assert!(!matches(b"foo\n!foo/bar\nfoo/bar/baz", "foo/bar/quux"));
417 assert!(!matches(b"foo/*\n!foo/bar", "foo/bar"));
418 }
419
420 #[test]
421 fn test_gitignore_file_ordering() {
422 let file1 = GitIgnoreFile::empty()
423 .chain("", Path::new(""), b"/foo\n")
424 .unwrap();
425 let file2 = file1.chain("foo/", Path::new(""), b"!/bar").unwrap();
426 let file3 = file2.chain("foo/bar/", Path::new(""), b"/baz").unwrap();
427 assert!(file1.matches("foo"));
428 assert!(file1.matches("foo/bar"));
429 assert!(!file2.matches("foo/bar"));
430 assert!(!file2.matches("foo/bar/baz"));
431 assert!(file2.matches("foo/baz"));
432 assert!(file3.matches("foo/bar/baz"));
433 assert!(!file3.matches("foo/bar/qux"));
434 }
435
436 #[test]
437 fn test_gitignore_negative_parent_directory() {
438 let ignore = GitIgnoreFile::empty()
451 .chain("", Path::new(""), b"foo/bar.*\n!/foo/\n")
452 .unwrap();
453 assert!(ignore.matches("foo/bar.ext"));
454
455 let ignore = GitIgnoreFile::empty()
456 .chain("", Path::new(""), b"!/foo/\nfoo/bar.*\n")
457 .unwrap();
458 assert!(ignore.matches("foo/bar.ext"));
459 }
460}