use std::path::Path;
use ignore::gitignore::{Gitignore, GitignoreBuilder};
pub fn read_ignore_patterns(path: &Path) -> Option<Vec<String>> {
let Ok(text) = std::fs::read_to_string(path) else {
return None;
};
let lines: Vec<String> = text
.lines()
.filter(|line| {
let trimmed = line.trim();
!trimmed.is_empty() && !trimmed.starts_with('#')
})
.map(|s| s.trim().to_string())
.collect();
if lines.is_empty() { None } else { Some(lines) }
}
pub fn read_ignore_file(path: &Path) -> Option<Gitignore> {
let patterns = read_ignore_patterns(path)?;
let root = path.parent().unwrap_or(Path::new("."));
let mut builder = GitignoreBuilder::new(root);
for line in &patterns {
let _ = builder.add_line(None, line);
}
builder.build().ok()
}
#[allow(dead_code)]
pub fn load_gitignore(directory: &Path) -> Option<Gitignore> {
let gitignore = directory.join(".gitignore");
if !gitignore.is_file() {
return None;
}
read_ignore_file(&gitignore)
}