use std::cell::OnceCell;
use crate::{Actor, Error, ModifiedFile, Repository};
pub struct Commit<'repo> {
inner: git2::Commit<'repo>,
ctx: &'repo Repository,
cache: OnceCell<git2::Diff<'repo>>,
}
impl<'repo> Commit<'repo> {
pub fn new(commit: git2::Commit<'repo>, repository: &'repo Repository) -> Self {
Self {
inner: commit.to_owned(),
ctx: repository,
cache: OnceCell::new(),
}
}
pub fn hash(&self) -> String {
self.inner.id().to_string()
}
pub fn msg(&self) -> Option<String> {
self.inner.message().map(|s| s.to_string())
}
pub fn author(&self) -> Actor {
Actor::new(self.inner.author())
}
pub fn committer(&self) -> Actor {
Actor::new(self.inner.committer())
}
pub fn parents(&self) -> impl Iterator<Item = String> {
self.inner.parent_ids().map(|id| id.to_string())
}
pub fn is_merge(&self) -> bool {
self.inner.parent_count() > 1
}
pub fn mod_files(&self) -> Result<impl Iterator<Item = ModifiedFile<'_>>, Error> {
let diff = self.diff()?;
Ok((0..diff.deltas().len()).map(move |n| ModifiedFile::new(diff, n)))
}
pub fn insertions(&self) -> Result<usize, Error> {
Ok(self.stats()?.insertions())
}
pub fn deletions(&self) -> Result<usize, Error> {
Ok(self.stats()?.deletions())
}
pub fn lines(&self) -> Result<usize, Error> {
Ok(self.insertions()? + self.deletions()?)
}
pub fn files(&self) -> Result<usize, Error> {
Ok(self.stats()?.files_changed())
}
fn stats(&self) -> Result<git2::DiffStats, Error> {
let diff = self.diff()?;
diff.stats().map_err(Error::Git)
}
fn diff(&self) -> Result<&git2::Diff<'repo>, Error> {
let diff = self.calculate_diff()?;
Ok(self.cache.get_or_init(|| diff))
}
fn calculate_diff(&self) -> Result<git2::Diff<'repo>, Error> {
let this_tree = self.inner.tree().ok();
let parent_tree = self.resolve_parent_tree()?;
self.ctx
.raw()
.diff_tree_to_tree(parent_tree.as_ref(), this_tree.as_ref(), None)
.map_err(Error::Git)
}
fn resolve_parent_tree(&self) -> Result<Option<git2::Tree<'_>>, Error> {
Ok(match self.inner.parent_count() {
0 => None,
1 => self.inner.parent(0).map_err(Error::Git)?.tree().ok(),
_ => return Err(Error::PathError("Placeholder error".to_string())),
})
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::{
Local, Repository,
common::{EXPECTED_ACTOR_EMAIL, EXPECTED_ACTOR_NAME, EXPECTED_MSG, init_repo},
};
fn commit_fixture<F, R>(f: F) -> R
where
F: FnOnce(&Repository<Local>, &Commit) -> R,
{
let repo = init_repo();
let repo = Repository::<Local>::from_repository(repo);
let commit = repo.head().expect("Failed to get HEAD");
f(&repo, &commit)
}
#[test]
fn test_msg() {
commit_fixture(|_, commit| {
assert_eq!(commit.msg(), Some(EXPECTED_MSG.to_owned()));
});
}
#[test]
fn test_author() {
commit_fixture(|_, commit| {
assert_eq!(
commit.author().name().unwrap(),
EXPECTED_ACTOR_NAME.to_string()
);
assert_eq!(
commit.author().email().unwrap(),
EXPECTED_ACTOR_EMAIL.to_string()
);
});
}
#[test]
fn test_committer() {
commit_fixture(|_, commit| {
assert_eq!(
commit.committer().name().unwrap(),
EXPECTED_ACTOR_NAME.to_string()
);
assert_eq!(
commit.committer().email().unwrap(),
EXPECTED_ACTOR_EMAIL.to_string()
);
});
}
#[test]
fn test_parents() {
commit_fixture(|_, commit| {
assert_eq!(commit.parents().collect::<Vec<String>>().len(), 1);
});
}
#[test]
fn test_is_merge() {
commit_fixture(|_, commit| {
assert!(!commit.is_merge());
});
}
#[test]
fn test_insertions() {
commit_fixture(|_, commit| {
assert_eq!(commit.insertions().unwrap(), 1);
});
}
#[test]
fn test_deletions() {
commit_fixture(|_, commit| {
assert_eq!(commit.deletions().unwrap(), 0);
});
}
#[test]
fn test_lines() {
commit_fixture(|_, commit| {
assert_eq!(commit.lines().unwrap(), 1);
});
}
#[test]
fn test_stat() {
commit_fixture(|_, commit| {
let _: git2::DiffStats = commit
.stats()
.expect("Failed to construct git2 Stats object");
});
}
}