diff options
author | Toby Vincent <tobyv13@gmail.com> | 2023-05-06 16:28:08 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2023-05-06 16:30:58 -0500 |
commit | bcdcdb0ada8d13950e22909250b6b6783866535e (patch) | |
tree | 834b5a6a2c9a2d10b87024103f387edddc0f76ba /src | |
parent | e554e7033320456762a82b8276e0137592d57dcb (diff) |
fix: use path_buf parser (mtime) only as fallback
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 2 | ||||
-rw-r--r-- | src/git.rs | 23 | ||||
-rw-r--r-- | src/path.rs | 13 | ||||
-rw-r--r-- | src/project.rs | 22 |
4 files changed, 35 insertions, 25 deletions
diff --git a/src/config.rs b/src/config.rs index aa9a6c4..c45036c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -49,7 +49,7 @@ pub struct Projects { /// /// Uses the directory mtime as the timestamp. #[arg(short, long)] - pub all: bool, + pub mtime: bool, /// Match directories containing <PATTERN>. /// @@ -19,7 +19,7 @@ impl Parser for Repository { type Error = Error; fn parse(&self, path_buf: PathBuf) -> Result<Project, Self::Error> { - let timestamp = self + let opt_commit = self .branches(Some(BranchType::Local))? .map(|branch| -> Result<_, Error> { let (branch, _) = branch?; @@ -28,9 +28,7 @@ impl Parser for Repository { .name()? .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) + self.revparse_single(name)?.peel_to_commit() }) .inspect(|res| { if let Err(err) = res { @@ -38,9 +36,20 @@ impl Parser for Repository { } }) .flatten() - .max() - .map(Duration::from_secs) - .unwrap_or_default(); + .max_by_key(|c| c.time().seconds() as u64); + + let timestamp = match opt_commit { + Some(c) => { + let time = c.time().seconds(); + let id = c.id(); + tracing::debug!(?path_buf, ?id, ?time, "Latest commit"); + Duration::from_secs(c.time().seconds() as u64) + } + None => { + tracing::warn!(?path_buf, "No commit found, using default"); + Duration::default() + } + }; Ok(Project { path_buf, diff --git a/src/path.rs b/src/path.rs index 1000b04..219e274 100644 --- a/src/path.rs +++ b/src/path.rs @@ -3,20 +3,17 @@ use std::{io::ErrorKind, path::PathBuf}; use crate::{parser::Parser, project::Project}; #[derive(Debug, Clone)] -pub enum PathMatcher { - All, - Pattern(String), -} +pub struct PathMatcher(pub String); impl Parser for PathMatcher { type Error = std::io::Error; #[tracing::instrument] fn parse(&self, path_buf: PathBuf) -> Result<Project, Self::Error> { - match self { - PathMatcher::All => path_buf.try_into(), - PathMatcher::Pattern(p) if path_buf.join(p).exists() => path_buf.try_into(), - _ => Err(std::io::Error::from(ErrorKind::NotFound)), + if !path_buf.join(&self.0).exists() { + return Err(std::io::Error::from(ErrorKind::NotFound)); } + + path_buf.try_into() } } diff --git a/src/project.rs b/src/project.rs index 7351ae4..5f98ef6 100644 --- a/src/project.rs +++ b/src/project.rs @@ -15,11 +15,15 @@ use crate::{parser::FilterMap, path::PathMatcher}; pub struct Projects { inner: HashMap<PathBuf, Duration>, filters: Vec<Box<dyn FilterMap>>, + mtime: bool, } impl Projects { - pub fn new() -> Self { - Self::default() + pub fn new(fallback: bool) -> Self { + Self { + mtime: fallback, + ..Default::default() + } } pub fn add_filter<T: FilterMap + 'static>(&mut self, filter: T) { @@ -77,8 +81,12 @@ impl Extend<PathBuf> for Projects { T: IntoIterator<Item = PathBuf>, { for path_buf in iter { - if let Some(project) = self.filters.filter_map(path_buf) { + if let Some(project) = self.filters.filter_map(path_buf.to_owned()) { self.insert(project) + } else if self.mtime { + if let Ok(project) = Project::try_from(path_buf) { + self.insert(project) + } } } } @@ -97,14 +105,10 @@ impl Extend<Project> for Projects { impl From<crate::config::Projects> for Projects { fn from(value: crate::config::Projects) -> Self { - let mut projects = Projects::new(); - - if value.all { - projects.add_filter(PathMatcher::All); - } + let mut projects = Projects::new(value.mtime); if let Some(pattern) = &value.pattern { - projects.add_filter(PathMatcher::Pattern(pattern.to_owned())); + projects.add_filter(PathMatcher(pattern.to_owned())); } #[cfg(feature = "git")] |