use std::{collections::BTreeSet, path::Path};
use crate::core::content_hash::is_regenerable;
fn is_local_environment(name: &str) -> bool {
let template = matches!(name, "example.env" | "sample.env" | "template.env")
|| name.ends_with(".example")
|| name.ends_with(".sample")
|| name.ends_with(".template");
!template && (name == ".env" || name.starts_with(".env.") || name.ends_with(".env"))
}
pub(super) fn ignored_paths(root: &Path, files: &[String]) -> Vec<String> {
let Ok(repo) = git2::Repository::discover(root) else {
return Vec::new();
};
let Some(raw_workdir) = repo.workdir() else {
return Vec::new();
};
let workdir = std::fs::canonicalize(raw_workdir).unwrap_or_else(|_| raw_workdir.to_path_buf());
let Ok(index) = repo.index() else {
return Vec::new();
};
let mut ignored = BTreeSet::new();
for path in files {
let shared_root = path
.split('/')
.fold(root.to_path_buf(), |acc, part| acc.join(part));
for entry in walkdir::WalkDir::new(&shared_root)
.follow_root_links(false)
.into_iter()
.filter_entry(|entry| {
let name = entry.file_name().to_string_lossy();
!is_regenerable(&name)
&& !(entry.file_type().is_file() && is_local_environment(&name))
})
.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_file() || entry.file_type().is_symlink())
{
let Ok(relative) = entry.path().strip_prefix(&workdir) else {
continue;
};
if index.get_path(relative, 0).is_some()
|| !repo.is_path_ignored(relative).unwrap_or(false)
{
continue;
}
let blocked = entry
.path()
.ancestors()
.take_while(|ancestor| ancestor.starts_with(&shared_root))
.filter(|ancestor| {
ancestor
.strip_prefix(&workdir)
.ok()
.is_some_and(|path| repo.is_path_ignored(path).unwrap_or(false))
})
.last()
.unwrap_or(entry.path());
if let Ok(relative) = blocked.strip_prefix(root) {
ignored.insert(relative.to_string_lossy().replace('\\', "/"));
}
}
}
ignored.into_iter().collect()
}
#[cfg(test)]
mod tests;