diff --git a/git-date/src/parse.rs b/git-date/src/parse.rs index 814cacd2c30..98ca53e9d69 100644 --- a/git-date/src/parse.rs +++ b/git-date/src/parse.rs @@ -103,27 +103,29 @@ mod relative { } pub(crate) fn parse(input: &str, now: Option) -> Option> { - parse_inner(input).map(|offset| { - let offset = std::time::Duration::from_secs(offset.whole_seconds().try_into().expect("positive value")); - now.ok_or(Error::MissingCurrentTime).map(|now| { - now.checked_sub(offset) - .expect("BUG: values can't be large enough to cause underflow") - .into() - }) + let offset = parse_inner(input).map(|offset| { + let secs = offset.whole_seconds().try_into().expect("positive value"); + return std::time::Duration::from_secs(secs); + })?; + now.ok_or(Error::MissingCurrentTime).map(|now| { + now.checked_sub(offset) + .expect("BUG: values can't be large enough to cause underflow") + .into() }) } fn duration(period: &str, multiplier: i64) -> Option { let period = period.strip_suffix('s').unwrap_or(period); - Some(match period { - "second" => Duration::seconds(multiplier), - "minute" => Duration::minutes(multiplier), - "hour" => Duration::hours(multiplier), - "day" => Duration::days(multiplier), - "week" => Duration::weeks(multiplier), - // TODO months & years + let seconds: i64 = match period { + "second" => 1, + "minute" => 60, + "hour" => 3_600, + "day" => 86_400, + "week" => 604_800, + // TODO months & years? _ => return None, - }) + }; + Some(Duration::seconds(seconds.checked_mul(multiplier)?)) } #[cfg(test)] diff --git a/git-date/tests/fixtures/generate_git_date_baseline.sh b/git-date/tests/fixtures/generate_git_date_baseline.sh index 0d76ede8e83..6c02cf7620d 100644 --- a/git-date/tests/fixtures/generate_git_date_baseline.sh +++ b/git-date/tests/fixtures/generate_git_date_baseline.sh @@ -39,8 +39,3 @@ baseline '123456789' # raw baseline '1660874655 +0800' -# failing - -# empty_input -baseline "" - diff --git a/git-date/tests/fixtures/generated-archives/generate_git_date_baseline.tar.xz b/git-date/tests/fixtures/generated-archives/generate_git_date_baseline.tar.xz index 29ce18a37dc..28a92ba796f 100644 Binary files a/git-date/tests/fixtures/generated-archives/generate_git_date_baseline.tar.xz and b/git-date/tests/fixtures/generated-archives/generate_git_date_baseline.tar.xz differ diff --git a/git-date/tests/time/parse.rs b/git-date/tests/time/parse.rs index f8eb67f0fda..259243bf621 100644 --- a/git-date/tests/time/parse.rs +++ b/git-date/tests/time/parse.rs @@ -6,7 +6,7 @@ use once_cell::sync::Lazy; type Result = std::result::Result>; -static BASELINE: Lazy> = Lazy::new(|| { +static BASELINE: Lazy> = Lazy::new(|| { let base = git_testtools::scripted_fixture_repo_read_only("generate_git_date_baseline.sh").unwrap(); (|| -> Result<_> { @@ -15,7 +15,14 @@ static BASELINE: Lazy> = Lazy::new(|| { let mut lines = baseline.lines(); while let Some(date_str) = lines.next() { let exit_code = lines.next().expect("three lines per baseline").to_str()?.parse()?; - let output = lines.next().expect("three lines per baseline").into(); + let output = u32::from_str( + lines + .next() + .expect("three lines per baseline") + .to_str() + .expect("valid utf"), + ) + .expect("valid epoch value"); map.insert(date_str.into(), (exit_code, output)); } Ok(map) @@ -34,8 +41,7 @@ fn baseline() { ); if *exit_code == 0 { let actual = res.unwrap().seconds_since_unix_epoch; - let expected = u32::from_str(output.to_str().expect("valid utf")).expect("valid epoch value"); - assert_eq!(actual, expected, "{pattern:?} disagrees with baseline: {actual:?}") + assert_eq!(actual, *output, "{pattern:?} disagrees with baseline: {actual:?}") } } } @@ -93,7 +99,6 @@ mod relative { use time::{Duration, OffsetDateTime}; #[test] - #[should_panic] // TODO: fix fn large_offsets_can_panic() { git_date::parse("999999999999999 weeks ago", Some(std::time::UNIX_EPOCH)).ok(); } diff --git a/git-prompt/src/unix.rs b/git-prompt/src/unix.rs index f48f7d269b8..a08d2d77777 100644 --- a/git-prompt/src/unix.rs +++ b/git-prompt/src/unix.rs @@ -9,11 +9,11 @@ pub(crate) mod imp { }; use nix::sys::{termios, termios::Termios}; - use parking_lot::{lock_api::MutexGuard, Mutex, RawMutex}; + use parking_lot::{const_mutex, lock_api::MutexGuard, Mutex, RawMutex}; use crate::{unix::TTY_PATH, Error, Mode, Options}; - static TERM_STATE: Mutex> = Mutex::new(None); + static TERM_STATE: Mutex> = const_mutex(None); /// Ask the user given a `prompt`, returning the result. pub(crate) fn ask(prompt: &str, Options { mode, .. }: &Options<'_>) -> Result {