use std::path::Path;
use git2::{Commit, Repository};
use crate::logger::{Level, Log, Verbosity};
pub fn open_repository(path: &Path) -> Result<Repository, Log> {
match git2::Repository::open(path) {
Ok(r) => Ok(r),
Err(e) => Err(Log::new(
Level::Error,
format!("Can't open the git repository: {}", e),
Some(Verbosity::Normal),
)),
}
}
pub fn get_commit_from<'repo>(
repo: &'repo Repository,
commit_hash: &str,
) -> Result<Commit<'repo>, Log> {
let obj = repo.revparse_single(commit_hash).map_err(|e| {
Log::new(
Level::Error,
format!("Unable to parse commit hash \"{}\": {}", commit_hash, e),
None,
)
})?;
obj.peel_to_commit().map_err(|e| {
Log::new(
Level::Error,
format!("{} is not a commit: {}", commit_hash, e),
None,
)
})
}