diff options
Diffstat (limited to 'src/sorter.rs')
-rw-r--r-- | src/sorter.rs | 62 |
1 files changed, 0 insertions, 62 deletions
diff --git a/src/sorter.rs b/src/sorter.rs deleted file mode 100644 index 049b8d2..0000000 --- a/src/sorter.rs +++ /dev/null @@ -1,62 +0,0 @@ -use std::{cmp::Ordering, path::PathBuf}; - -use git2::{BranchType, Repository}; -use tracing::error; - -pub trait Sorter { - #[allow(clippy::ptr_arg)] - fn compare(a: &PathBuf, b: &PathBuf) -> Ordering; - - fn sort(vec: &mut Vec<PathBuf>) { - vec.sort_unstable_by(Self::compare); - } -} - -pub struct GitSorter; - -impl GitSorter { - fn get_commit(path: &PathBuf) -> Result<i64, git2::Error> { - let repo = Repository::open(path)?; - let mut branches = repo.branches(Some(BranchType::Local))?; - branches.try_fold(0, |latest, branch| { - let branch = branch?.0; - - let name = branch - .name()? - .ok_or_else(|| git2::Error::from_str("Failed to find branch"))?; - - repo.revparse_single(name)? - .peel_to_commit() - .map(|c| c.time().seconds().max(latest)) - }) - } -} - -impl Sorter for GitSorter { - fn compare(path_a: &PathBuf, path_b: &PathBuf) -> Ordering { - let commit_a = Self::get_commit(path_a); - let commit_b = Self::get_commit(path_b); - - match (commit_a, commit_b) { - (Ok(a), Ok(b)) => a.cmp(&b), - (Ok(_), Err(error_b)) => { - error!(?path_b, ?error_b, "Error while comparing git repos"); - Ordering::Less - } - (Err(error_a), Ok(_)) => { - error!(?path_a, ?error_a, "Error while comparing git repos"); - Ordering::Greater - } - (Err(error_a), Err(error_b)) => { - error!( - ?path_a, - ?error_a, - ?path_b, - ?error_b, - "Error while comparing git repos" - ); - Ordering::Equal - } - } - } -} |