use std::{
fs,
io::{BufRead, BufReader},
path::{Path, PathBuf},
};
use crate::evaluator::{File, Glob, Result};
pub fn read_gitignore(
base_path: Option<impl AsRef<Path>>,
gitignore_path: impl AsRef<Path>,
file: fs::File,
checksum: &[u8],
) -> Result<File> {
let base_path = base_path.map_or_else(
|| {
gitignore_path
.as_ref()
.parent()
.map_or_else(PathBuf::new, Path::to_path_buf)
},
|base_path| base_path.as_ref().to_path_buf(),
);
let reader = BufReader::new(file);
let mut content = Vec::<Glob>::new();
for line in reader.lines() {
let line = line.unwrap_or_default();
let glob = Glob::try_from(line.as_str())?;
if glob.is_empty() {
continue;
}
content.push(glob);
}
Ok(File::new(base_path, content, checksum.to_vec()))
}