2017-09-28 08:09:57 +00:00
|
|
|
|
//! Ignoring globs in `.gitignore` files.
|
|
|
|
|
//!
|
|
|
|
|
//! This uses a cache because the file with the globs in might not be the same
|
|
|
|
|
//! directory that we’re listing!
|
|
|
|
|
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::Read;
|
|
|
|
|
use std::path::{Path, PathBuf};
|
2017-09-28 15:13:47 +00:00
|
|
|
|
use std::sync::RwLock;
|
2017-09-28 08:09:57 +00:00
|
|
|
|
|
|
|
|
|
use fs::filter::IgnorePatterns;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// An **ignore cache** holds sets of glob patterns paired with the
|
2017-09-28 15:13:47 +00:00
|
|
|
|
/// directories that they should be ignored underneath. Believe it or not,
|
|
|
|
|
/// that’s a valid English sentence.
|
2017-09-28 08:09:57 +00:00
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
|
pub struct IgnoreCache {
|
2017-09-28 15:13:47 +00:00
|
|
|
|
entries: RwLock<Vec<(PathBuf, IgnorePatterns)>>
|
2017-09-28 08:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IgnoreCache {
|
|
|
|
|
pub fn new() -> IgnoreCache {
|
|
|
|
|
IgnoreCache::default()
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-28 15:13:47 +00:00
|
|
|
|
pub fn discover_underneath(&self, path: &Path) {
|
2017-09-28 08:09:57 +00:00
|
|
|
|
let mut path = Some(path);
|
2017-09-28 15:13:47 +00:00
|
|
|
|
let mut entries = self.entries.write().unwrap();
|
2017-09-28 08:09:57 +00:00
|
|
|
|
|
|
|
|
|
while let Some(p) = path {
|
2017-09-28 15:13:47 +00:00
|
|
|
|
if p.components().next().is_none() { break }
|
|
|
|
|
|
2017-09-28 08:09:57 +00:00
|
|
|
|
let ignore_file = p.join(".gitignore");
|
|
|
|
|
if ignore_file.is_file() {
|
2017-09-28 15:13:47 +00:00
|
|
|
|
debug!("Found a .gitignore file: {:?}", ignore_file);
|
2017-09-28 08:09:57 +00:00
|
|
|
|
if let Ok(mut file) = File::open(ignore_file) {
|
|
|
|
|
let mut contents = String::new();
|
|
|
|
|
|
2017-09-28 16:46:30 +00:00
|
|
|
|
match file.read_to_string(&mut contents) {
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
let (patterns, mut _errors) = IgnorePatterns::parse_from_iter(contents.lines());
|
|
|
|
|
entries.push((p.into(), patterns));
|
|
|
|
|
}
|
|
|
|
|
Err(e) => debug!("Failed to read a .gitignore: {:?}", e)
|
|
|
|
|
}
|
2017-09-28 08:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-28 15:13:47 +00:00
|
|
|
|
else {
|
|
|
|
|
debug!("Found no .gitignore file at {:?}", ignore_file);
|
|
|
|
|
}
|
2017-09-28 08:09:57 +00:00
|
|
|
|
|
|
|
|
|
path = p.parent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_ignored(&self, suspect: &Path) -> bool {
|
2017-09-28 15:13:47 +00:00
|
|
|
|
let entries = self.entries.read().unwrap();
|
|
|
|
|
entries.iter().any(|&(ref base_path, ref patterns)| {
|
2017-09-28 08:09:57 +00:00
|
|
|
|
if let Ok(suffix) = suspect.strip_prefix(&base_path) {
|
|
|
|
|
patterns.is_ignored_path(suffix)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn empty() {
|
|
|
|
|
let ignores = IgnoreCache::default();
|
|
|
|
|
assert_eq!(false, ignores.is_ignored(Path::new("/usr/bin/drinking")));
|
|
|
|
|
assert_eq!(false, ignores.is_ignored(Path::new("target/debug/exa")));
|
|
|
|
|
}
|
|
|
|
|
}
|