blob: 72f9285369995b0f5cca49cb4c2d30b5042ca340 (
plain)
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
41
42
43
|
//! See `Include`.
use std::convert::TryFrom;
use globset::{Glob, GlobSet, GlobSetBuilder};
use paths::{RelPath, RelPathBuf};
/// `Include` is the opposite of .gitignore.
///
/// It describes the set of files inside some directory.
///
/// The current implementation is very limited, it allows including file globs
/// and recursively excluding directories.
#[derive(Debug, Clone)]
pub(crate) struct Include {
include_files: GlobSet,
exclude_dirs: Vec<RelPathBuf>,
}
impl Include {
pub(crate) fn new(include: Vec<String>) -> Include {
let mut include_files = GlobSetBuilder::new();
let mut exclude_dirs = Vec::new();
for glob in include {
if glob.starts_with("!/") {
if let Ok(path) = RelPathBuf::try_from(&glob["!/".len()..]) {
exclude_dirs.push(path)
}
} else {
include_files.add(Glob::new(&glob).unwrap());
}
}
let include_files = include_files.build().unwrap();
Include { include_files, exclude_dirs }
}
pub(crate) fn include_file(&self, path: &RelPath) -> bool {
self.include_files.is_match(path)
}
pub(crate) fn exclude_dir(&self, path: &RelPath) -> bool {
self.exclude_dirs.iter().any(|excluded| path.starts_with(excluded))
}
}
|