Skip to content

Commit da7fc17

Browse files
committed
feat: add date filtering options for commit history
1 parent 5467d70 commit da7fc17

6 files changed

Lines changed: 137 additions & 8 deletions

File tree

‎Cargo.lock‎

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ tree-sitter-yaml = "0.7"
5656
tree-sitter-xml = "0.7"
5757
tree-sitter-bash = "0.25"
5858
chrono = "0.4"
59+
chrono-english = "0.1"
5960
rand = "0.9"
6061
serde = { version = "1.0", features = ["derive"] }
6162
toml = "0.9"

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ gitlogue --commit HEAD~10..HEAD --loop
9595
# Filter commits by author or email (case-insensitive partial match)
9696
gitlogue --author "john"
9797

98+
# Filter commits by date
99+
gitlogue --after "2024-01-01"
100+
gitlogue --before "1 week ago"
101+
gitlogue --after "2024-06-01" --before "2024-07-01"
102+
98103
# Use a different theme
99104
gitlogue --theme dracula
100105

‎docs/usage.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ When using author filtering:
9898
- Use `--loop` to replay continuously
9999
- Use `--order` to change playback order
100100

101+
### `--before <DATE>` / `--after <DATE>`
102+
103+
Filter commits by date. Supports absolute dates and relative expressions.
104+
105+
```bash
106+
# Absolute dates
107+
gitlogue --after "2024-01-01"
108+
gitlogue --before "2024-12-31"
109+
110+
# Relative dates
111+
gitlogue --after "1 week ago"
112+
gitlogue --before "yesterday"
113+
gitlogue --after "3 months ago"
114+
115+
# Date range
116+
gitlogue --after "2024-06-01" --before "2024-07-01"
117+
118+
# Combine with author filter
119+
gitlogue --author "john" --after "2024-01-01"
120+
```
121+
122+
Supported date formats:
123+
- Absolute: `2024-01-01`, `January 15, 2024`, `15 Jan 2024`
124+
- Relative: `yesterday`, `3 days ago`, `1 week ago`, `2 months ago`, `1 year ago`
125+
101126
### `--theme <NAME>`
102127

103128
Select a theme for the UI.

‎src/git.rs‎

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::{Context, Result};
2-
use chrono::{DateTime, Utc};
2+
use chrono::{DateTime, Local, Utc};
3+
use chrono_english::{parse_date_string, Dialect};
34
use git2::{Commit as Git2Commit, Delta, DiffOptions, Oid, Repository};
45
use globset::{Glob, GlobSet, GlobSetBuilder};
56
use 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+
133167
pub 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
}

‎src/main.rs‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ pub struct Args {
105105
)]
106106
pub author: Option<String>,
107107

108+
#[arg(
109+
long,
110+
value_name = "DATE",
111+
help = "Show commits before this date (e.g., '2024-01-01', '1 week ago', 'yesterday')"
112+
)]
113+
pub before: Option<String>,
114+
115+
#[arg(
116+
long,
117+
value_name = "DATE",
118+
help = "Show commits after this date (e.g., '2024-01-01', '1 week ago', 'yesterday')"
119+
)]
120+
pub after: Option<String>,
121+
108122
#[arg(
109123
short = 'i',
110124
long = "ignore",
@@ -230,13 +244,24 @@ fn main() -> Result<()> {
230244
repo.set_author_filter(args.author.clone());
231245
}
232246

247+
// Set date filters if specified
248+
if let Some(ref before_str) = args.before {
249+
let before_date = git::parse_date(before_str)?;
250+
repo.set_before_filter(Some(before_date));
251+
}
252+
if let Some(ref after_str) = args.after {
253+
let after_date = git::parse_date(after_str)?;
254+
repo.set_after_filter(Some(after_date));
255+
}
256+
233257
let is_commit_specified = args.commit.is_some();
234258
let is_range_mode = args
235259
.commit
236260
.as_ref()
237261
.map(|c| c.contains(".."))
238262
.unwrap_or(false);
239-
let is_author_filtered = args.author.is_some();
263+
let is_filtered =
264+
args.author.is_some() || args.before.is_some() || args.after.is_some();
240265

241266
// Load config: CLI arguments > config file > defaults
242267
let config = Config::load()?;
@@ -265,7 +290,7 @@ fn main() -> Result<()> {
265290
});
266291

267292
// Filtered modes default to asc (chronological) if not explicitly specified
268-
if (is_range_mode || is_author_filtered) && args.order.is_none() {
293+
if (is_range_mode || is_filtered) && args.order.is_none() {
269294
order = PlaybackOrder::Asc;
270295
}
271296

@@ -300,8 +325,8 @@ fn main() -> Result<()> {
300325
};
301326

302327
// Create UI with repository reference
303-
// Filtered modes (range/author) always need repo ref for iteration
304-
let repo_ref = if is_range_mode || is_author_filtered {
328+
// Filtered modes (range/author/date) always need repo ref for iteration
329+
let repo_ref = if is_range_mode || is_filtered {
305330
Some(&repo)
306331
} else if is_commit_specified && !loop_playback {
307332
None

0 commit comments

Comments
 (0)