diff --git a/src/git/mod.rs b/src/git/mod.rs index e921a90..3503bd6 100644 --- a/src/git/mod.rs +++ b/src/git/mod.rs @@ -7,9 +7,17 @@ pub(crate) mod repo; pub(crate) mod tree; use chrono::offset::{Local, TimeZone}; -use chrono::DateTime; +use chrono::{DateTime, NaiveDateTime}; use git2::{Commit, Signature}; +fn min_date() -> DateTime { + Local.from_utc_datetime(&NaiveDateTime::MIN) +} + +fn timestamp_with_fallback(timestamp: i64) -> DateTime { + Local.timestamp_opt(timestamp, 0).earliest().unwrap_or(min_date()) +} + #[derive(Clone, Debug)] pub struct SocialSignature { pub when: DateTime, @@ -20,7 +28,7 @@ pub struct SocialSignature { impl Default for SocialSignature { fn default() -> Self { Self { - when: Local.timestamp(0, 0), + when: min_date(), name: "".to_string(), email: "".to_string(), } @@ -53,7 +61,7 @@ impl Default for SocialCommit { fn default() -> Self { Self { id: "".to_string(), - time: Local.timestamp(0, 0), + time: min_date(), message: "".to_string(), author: SocialSignature::default(), committer: SocialSignature::default(), @@ -68,7 +76,7 @@ impl From> for SocialSignature { Self { name: s.name().unwrap_or("").to_owned(), email: s.email().unwrap_or("").to_owned(), - when: Local.timestamp(s.when().seconds(), 0), + when: timestamp_with_fallback(s.when().seconds()), } } } @@ -77,7 +85,7 @@ impl From> for SocialCommit { fn from(c: Commit<'_>) -> Self { Self { id: format!("{}", c.id()), - time: Local.timestamp(c.time().seconds(), 0), + time: timestamp_with_fallback(c.time().seconds()), message: c.message().unwrap_or("").to_owned(), author: c.author().into(), committer: c.committer().into(),