use super::repo::{git, split_nul};
use std::path::Path;
pub fn tracked(root: &Path, bases: &[String]) -> Option<Vec<String>> {
let mut args = vec!["ls-files", "-z", "--recurse-submodules", "--"];
args.extend(bases.iter().map(String::as_str));
git(root, &args).map(split_nul)
}
#[derive(Debug, Default)]
pub struct TrackedSet {
files: std::collections::HashSet<String>,
dirs: std::collections::HashSet<String>,
}
impl TrackedSet {
pub fn load(root: &Path) -> Option<Self> {
let files = tracked(root, &[])?;
let mut set = TrackedSet::default();
for f in files {
let mut dir = f.as_str();
while let Some((up, _)) = dir.rsplit_once('/') {
if !set.dirs.insert(up.to_string()) {
break;
}
dir = up;
}
set.files.insert(f);
}
Some(set)
}
pub fn has_file(&self, rel: &str) -> bool {
self.files.contains(rel)
}
pub fn has_dir(&self, rel: &str) -> bool {
self.dirs.contains(rel)
}
}
pub fn past_blobs(root: &Path, path: &str) -> Vec<String> {
let Some(out) = git(root, &["log", "--format=", "--raw", "--no-abbrev", "--", path]) else { return vec![] };
let mut ids = vec![];
for line in String::from_utf8_lossy(&out).lines().filter(|l| l.starts_with(':')) {
let f: Vec<&str> = line.split_whitespace().collect();
for id in f.iter().skip(2).take(2) {
if id.chars().any(|c| c != '0') && !ids.contains(&id.to_string()) {
ids.push(id.to_string());
}
}
}
ids
}
pub fn blob_id(root: &Path, path: &str) -> Option<String> {
git(root, &["hash-object", "--", path]).map(|o| String::from_utf8_lossy(&o).trim().to_string())
}
pub fn committed(root: &Path) -> Option<Vec<String>> {
git(root, &["ls-tree", "-r", "-z", "--name-only", "HEAD"]).map(split_nul)
}
pub fn ignored(root: &Path, paths: &[String]) -> Vec<String> {
if paths.is_empty() {
return vec![];
}
let mut args = vec!["check-ignore", "--"];
args.extend(paths.iter().map(String::as_str));
git(root, &args).map(|o| String::from_utf8_lossy(&o).lines().map(str::to_string).collect()).unwrap_or_default()
}