use git2::{Delta, Diff, DiffDelta, Patch};
use std::{path::Path, sync::OnceLock};
use crate::Error;
pub struct ModifiedFile<'c> {
cache: OnceLock<Option<Patch<'c>>>,
diff: &'c Diff<'c>,
n: usize,
}
impl<'c> ModifiedFile<'c> {
pub fn new(diff: &'c Diff<'_>, n: usize) -> Self {
ModifiedFile {
cache: OnceLock::new(),
diff,
n,
}
}
pub fn old_path(&self) -> Option<&Path> {
self.delta()?.old_file().path()
}
pub fn new_path(&self) -> Option<&Path> {
self.delta()?.new_file().path()
}
pub fn filename(&self) -> Option<&str> {
let dev_null = Path::new("/dev/null");
let path = match self.new_path() {
Some(p) if p != dev_null => p,
_ => self.old_path()?,
};
path.file_name()?.to_str()
}
pub fn status(&self) -> Option<Delta> {
Some(self.delta()?.status())
}
pub fn insertions(&self) -> Result<usize, Error> {
Ok(match self.patch()? {
Some(p) => p.line_stats()?.1,
None => 0,
})
}
pub fn deletions(&self) -> Result<usize, Error> {
Ok(match self.patch()? {
Some(p) => p.line_stats()?.2,
None => 0,
})
}
fn delta(&self) -> Option<DiffDelta<'_>> {
self.diff.get_delta(self.n)
}
fn patch(&self) -> Result<Option<&Patch<'_>>, Error> {
let patch = Patch::from_diff(self.diff, self.n)?;
Ok(self.cache.get_or_init(|| patch).as_ref())
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::common::init_repo;
fn mfile_fixture<F, R>(f: F) -> R
where
F: FnOnce(&git2::Diff, &ModifiedFile) -> R,
{
let repo = init_repo();
let c1 = repo
.head()
.expect("Failed to fetch HEAD")
.peel_to_commit()
.expect("Failed to peel HEAD to commit");
let c2 = c1.parent(0).expect("Couldn't get parent");
let diff = repo
.diff_tree_to_tree(
Some(&c2.tree().expect("Failed to get tree")),
Some(&c1.tree().expect("Failed to get tree")),
None,
)
.expect("Failed to make diff");
let mfile = ModifiedFile::new(&diff, 0);
f(&diff, &mfile)
}
#[test]
fn test_old_path() {
mfile_fixture(|_, mfile| {
assert_eq!(mfile.old_path().unwrap(), "file.txt");
});
}
#[test]
fn test_new_path() {
mfile_fixture(|_, mfile| {
assert_eq!(mfile.new_path().unwrap(), "file.txt");
});
}
#[test]
fn test_delta() {
mfile_fixture(|_, mfile| {
assert_eq!(mfile.new_path().unwrap(), "file.txt");
});
}
}