11use anyhow:: { Context , Result } ;
2- use chrono:: { DateTime , Utc } ;
2+ use chrono:: { DateTime , Local , Utc } ;
3+ use chrono_english:: { parse_date_string, Dialect } ;
34use git2:: { Commit as Git2Commit , Delta , DiffOptions , Oid , Repository } ;
45use globset:: { Glob , GlobSet , GlobSetBuilder } ;
56use rand:: Rng ;
@@ -130,6 +131,39 @@ fn matches_author(commit: &Git2Commit, pattern: &str) -> bool {
130131 name. to_lowercase ( ) . contains ( & pattern_lower) || email. to_lowercase ( ) . contains ( & pattern_lower)
131132}
132133
134+ // Parse a date string using chrono-english (supports Git-like formats)
135+ pub fn parse_date ( input : & str ) -> Result < DateTime < Utc > > {
136+ let now = Local :: now ( ) ;
137+
138+ parse_date_string ( input, now, Dialect :: Us )
139+ . map ( |dt| dt. with_timezone ( & Utc ) )
140+ . with_context ( || format ! ( "Invalid date format: '{}'. Use formats like '2024-01-01', '1 week ago', 'yesterday'" , input) )
141+ }
142+
143+ // Check if a commit date is within the specified date range
144+ fn matches_date_filter (
145+ commit : & Git2Commit ,
146+ before : Option < & DateTime < Utc > > ,
147+ after : Option < & DateTime < Utc > > ,
148+ ) -> bool {
149+ let timestamp = commit. author ( ) . when ( ) . seconds ( ) ;
150+ let commit_date = DateTime :: from_timestamp ( timestamp, 0 ) . unwrap_or_else ( Utc :: now) ;
151+
152+ if let Some ( before_date) = before {
153+ if commit_date >= * before_date {
154+ return false ;
155+ }
156+ }
157+
158+ if let Some ( after_date) = after {
159+ if commit_date <= * after_date {
160+ return false ;
161+ }
162+ }
163+
164+ true
165+ }
166+
133167pub struct GitRepository {
134168 repo : Repository ,
135169 commit_cache : RefCell < Option < Vec < Oid > > > ,
@@ -138,6 +172,8 @@ pub struct GitRepository {
138172 commit_index : RefCell < usize > ,
139173 commit_range : RefCell < Option < Vec < Oid > > > ,
140174 author_filter : Option < String > ,
175+ before_filter : Option < DateTime < Utc > > ,
176+ after_filter : Option < DateTime < Utc > > ,
141177}
142178
143179#[ derive( Debug , Clone ) ]
@@ -264,6 +300,8 @@ impl GitRepository {
264300 commit_index : RefCell :: new ( 0 ) ,
265301 commit_range : RefCell :: new ( None ) ,
266302 author_filter : None ,
303+ before_filter : None ,
304+ after_filter : None ,
267305 } )
268306 }
269307
@@ -351,6 +389,14 @@ impl GitRepository {
351389 self . author_filter = author;
352390 }
353391
392+ pub fn set_before_filter ( & mut self , before : Option < DateTime < Utc > > ) {
393+ self . before_filter = before;
394+ }
395+
396+ pub fn set_after_filter ( & mut self , after : Option < DateTime < Utc > > ) {
397+ self . after_filter = after;
398+ }
399+
354400 pub fn set_commit_range ( & self , range : & str ) -> Result < ( ) > {
355401 let commits = self . parse_commit_range ( range) ?;
356402 * self . commit_range . borrow_mut ( ) = Some ( commits) ;
@@ -416,7 +462,7 @@ impl GitRepository {
416462 Self :: extract_metadata_with_changes ( & self . repo , & commit)
417463 }
418464
419- // Collect non-merge commits from a revwalk, applying author filter if set
465+ // Collect non-merge commits from a revwalk, applying author and date filters if set
420466 fn collect_commits_from_revwalk (
421467 & self ,
422468 revwalk : git2:: Revwalk ,
@@ -431,14 +477,24 @@ impl GitRepository {
431477 continue ;
432478 }
433479 }
480+ if !matches_date_filter (
481+ & commit,
482+ self . before_filter . as_ref ( ) ,
483+ self . after_filter . as_ref ( ) ,
484+ ) {
485+ continue ;
486+ }
434487 commits. push ( oid) ;
435488 }
436489 }
437490 }
438491
439492 if commits. is_empty ( ) {
440- if self . author_filter . is_some ( ) {
441- anyhow:: bail!( "No commits found matching the author filter {}" , context) ;
493+ if self . author_filter . is_some ( )
494+ || self . before_filter . is_some ( )
495+ || self . after_filter . is_some ( )
496+ {
497+ anyhow:: bail!( "No commits found matching the filters {}" , context) ;
442498 }
443499 anyhow:: bail!( "No non-merge commits found {}" , context) ;
444500 }
0 commit comments