use super::repo::{git, git_inner, nearest_repo, split_nul};
use std::collections::HashMap;
use std::path::Path;
pub fn staged(root: &Path) -> Option<Vec<String>> {
git(root, &["diff", "--cached", "--name-only", "-z", "--diff-filter=ACMR"]).map(split_nul)
}
pub fn changed_since(root: &Path, base: &str) -> Option<Vec<String>> {
let mut files = split_nul(git(root, &["diff", "--name-only", "-z", "--diff-filter=ACMR", base])?);
if let Some(untracked) = git(root, &["ls-files", "-z", "--others", "--exclude-standard"]) {
files.extend(split_nul(untracked));
}
files.sort();
files.dedup();
Some(files)
}
pub fn changed_in_range(root: &Path, range: &str) -> Option<Vec<String>> {
git(root, &["diff", "--name-only", "-z", "--diff-filter=ACMR", range]).map(split_nul)
}
pub fn gone(root: &Path, base: Option<&str>) -> Option<Vec<String>> {
let mut args = vec!["diff", "--name-status", "-z", "-M", "--diff-filter=DR"];
match base {
Some(b) => args.push(b),
None => args.push("--cached"),
}
let words = split_nul(git(root, &args)?);
let mut out = vec![];
let mut it = words.into_iter();
while let Some(status) = it.next() {
let Some(old) = it.next() else { break };
if status.starts_with('R') {
it.next();
}
out.push(old);
}
Some(out)
}
pub fn added_lines(root: &Path, base: Option<&str>) -> Option<HashMap<String, Vec<usize>>> {
let mut args = vec!["diff", "-U0", "--no-color", "--no-ext-diff", "--diff-filter=ACMR"];
match base {
Some(b) => args.push(b),
None => args.push("--cached"),
}
let out = git(root, &args)?;
let mut map: HashMap<String, Vec<usize>> = HashMap::new();
let mut current: Option<String> = None;
for line in String::from_utf8_lossy(&out).lines() {
if let Some(p) = line.strip_prefix("+++ ") {
current = p.strip_prefix("b/").map(str::to_string);
} else if let (Some(p), Some(h)) = (¤t, line.strip_prefix("@@ ")) {
let added = h.split(' ').find(|s| s.starts_with('+')).unwrap_or("+0,0");
let (start, count) = added[1..].split_once(',').unwrap_or((&added[1..], "1"));
let (start, count): (usize, usize) = (start.parse().unwrap_or(0), count.parse().unwrap_or(0));
map.entry(p.clone()).or_default().extend(start..start + count);
}
}
Some(map)
}
pub fn show(root: &Path, rev: &str, path: &str) -> Option<String> {
let outer = || git(root, &["show", &format!("{rev}:{path}")]);
let (repo, rel, prefix) = nearest_repo(root, path);
if prefix.is_empty() {
return outer().map(text);
}
let pin = pinned(root, &format!("{rev}:{}", prefix.trim_end_matches('/')));
let at = pin.as_deref().unwrap_or(rev);
git_inner(&repo, &["show", &format!("{at}:{rel}")]).or_else(outer).map(text)
}
pub fn show_staged(root: &Path, path: &str) -> Option<String> {
let outer = || git(root, &["show", &format!(":{path}")]);
let (repo, rel, prefix) = nearest_repo(root, path);
let Some(pin) = (!prefix.is_empty()).then(|| pinned(root, &format!(":{}", prefix.trim_end_matches('/')))).flatten()
else {
return outer().map(text);
};
git_inner(&repo, &["show", &format!("{pin}:{rel}")]).map(text)
}
fn pinned(root: &Path, spec: &str) -> Option<String> {
let out = git(root, &["rev-parse", "--verify", "--quiet", spec])?;
Some(String::from_utf8_lossy(&out).trim().to_string()).filter(|s| !s.is_empty())
}
fn text(bytes: Vec<u8>) -> String {
String::from_utf8_lossy(&bytes).into_owned()
}