diff options
Diffstat (limited to 'src/git.rs')
-rw-r--r-- | src/git.rs | 29 |
1 files changed, 20 insertions, 9 deletions
@@ -1,4 +1,4 @@ -use git2::{BranchType, Repository}; +use git2::{BranchType, Error, Repository}; use std::{path::PathBuf, time::Duration}; use crate::{parser::Parser, project::Project}; @@ -7,32 +7,43 @@ use crate::{parser::Parser, project::Project}; pub struct Git; impl Parser for Git { + type Error = Error; + #[tracing::instrument] - fn parse(&self, path_buf: PathBuf) -> Result<Project, Box<dyn std::error::Error>> { + fn parse(&self, path_buf: PathBuf) -> Result<Project, Self::Error> { Repository::open(&path_buf)?.parse(path_buf) } } impl Parser for Repository { - fn parse(&self, path_buf: PathBuf) -> Result<Project, Box<dyn std::error::Error>> { - let mut branches = self.branches(Some(BranchType::Local))?; - let timestamp = branches - .try_fold(0, |latest, branch| -> std::result::Result<u64, _> { + type Error = Error; + + fn parse(&self, path_buf: PathBuf) -> Result<Project, Self::Error> { + let timestamp = self + .branches(Some(BranchType::Local))? + .map(|branch| -> Result<_, Error> { let (branch, _) = branch?; let name = branch .name()? - .ok_or_else(|| git2::Error::from_str("Failed to find branch"))?; + .ok_or_else(|| Error::from_str("Failed to get branch name"))?; self.revparse_single(name)? .peel_to_commit() - .map(|c| (c.time().seconds() as u64).max(latest)) + .map(|c| c.time().seconds() as u64) + }) + .inspect(|res| { + if let Err(err) = res { + tracing::warn!(%err, "Skipping errored branch") + } }) + .flatten() + .max() .map(Duration::from_secs) .unwrap_or_default(); Ok(Project { - worktree: path_buf, + path_buf, timestamp, }) } |