use ignore::gitignore::Gitignore;
use std::path::Path;
const IGNORE_FILE_NAME: &str = ".sqruffignore";
pub struct IgnoreFile {
ignore: Gitignore,
}
impl IgnoreFile {
pub fn new_from_root(root: &Path) -> Result<Self, String> {
let ignore_file = root.join(IGNORE_FILE_NAME);
if ignore_file.exists() {
let ignore = Gitignore::new(ignore_file);
match ignore {
(ignore, None) => Ok(IgnoreFile { ignore }),
(_, Some(err)) => Err(err.to_string()),
}
} else {
Ok(Self::empty())
}
}
pub fn empty() -> Self {
Self {
ignore: Gitignore::empty(),
}
}
pub fn is_ignored(&self, path: &Path) -> bool {
let is_dir = path.is_dir();
let match_result = self.ignore.matched(path, is_dir);
let is_ignored = match_result.is_ignore();
if is_ignored {
let path_type = if is_dir { "directory" } else { "file" };
log::debug!(
"Ignoring {} '{}' due to ignore pattern",
path_type,
path.display()
);
if let Some(pattern) = match_result.inner() {
log::debug!("Matched ignore pattern: '{}'", pattern.original());
}
}
is_ignored
}
}