Skip to content

Commit 83a3832

Browse files
committed
Merge branch 'git_date_relative'
2 parents e10554d + c5c6bf6 commit 83a3832

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

git-date/src/parse.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,17 @@ mod relative {
115115

116116
fn duration(period: &str, multiplier: i64) -> Option<Duration> {
117117
let period = period.strip_suffix('s').unwrap_or(period);
118-
Some(match period {
119-
"second" => Duration::seconds(multiplier),
120-
"minute" => Duration::minutes(multiplier),
121-
"hour" => Duration::hours(multiplier),
122-
"day" => Duration::days(multiplier),
123-
"week" => Duration::weeks(multiplier),
124-
// TODO months & years
118+
let seconds: i64 = match period {
119+
"second" => 1,
120+
"minute" => 60,
121+
"hour" => 60 * 60,
122+
"day" => 24 * 60 * 60,
123+
"week" => 7 * 24 * 60 * 60,
124+
// TODO months & years? YES
125+
// Ignore values you don't know, assume seconds then (so does git)
125126
_ => return None,
126-
})
127+
};
128+
seconds.checked_mul(multiplier).map(Duration::seconds)
127129
}
128130

129131
#[cfg(test)]

git-date/tests/fixtures/generate_git_date_baseline.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,3 @@ baseline '123456789'
3939
# raw
4040
baseline '1660874655 +0800'
4141

42-
# failing
43-
44-
# empty_input
45-
baseline ""
46-
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:7495815a04eea1a5b657d944b85f2dac5bb73bbda43a409ba70a2094c23261cb
3-
size 9188
2+
oid sha256:43b9d4d43a1674b5de3ae752b7578ba6014f79ee136c868423819e15a94dff96
3+
size 9184

git-date/tests/time/parse.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::{collections::HashMap, str::FromStr, time::SystemTime};
1+
use std::{collections::HashMap, time::SystemTime};
22

33
use bstr::{BString, ByteSlice};
44
use git_date::{time::Sign, Time};
55
use once_cell::sync::Lazy;
66

77
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
88

9-
static BASELINE: Lazy<HashMap<BString, (usize, BString)>> = Lazy::new(|| {
9+
static BASELINE: Lazy<HashMap<BString, (usize, u32)>> = Lazy::new(|| {
1010
let base = git_testtools::scripted_fixture_repo_read_only("generate_git_date_baseline.sh").unwrap();
1111

1212
(|| -> Result<_> {
@@ -15,7 +15,13 @@ static BASELINE: Lazy<HashMap<BString, (usize, BString)>> = Lazy::new(|| {
1515
let mut lines = baseline.lines();
1616
while let Some(date_str) = lines.next() {
1717
let exit_code = lines.next().expect("three lines per baseline").to_str()?.parse()?;
18-
let output = lines.next().expect("three lines per baseline").into();
18+
let output: u32 = lines
19+
.next()
20+
.expect("three lines per baseline")
21+
.to_str()
22+
.expect("valid utf")
23+
.parse()
24+
.expect("valid epoch value");
1925
map.insert(date_str.into(), (exit_code, output));
2026
}
2127
Ok(map)
@@ -34,8 +40,7 @@ fn baseline() {
3440
);
3541
if *exit_code == 0 {
3642
let actual = res.unwrap().seconds_since_unix_epoch;
37-
let expected = u32::from_str(output.to_str().expect("valid utf")).expect("valid epoch value");
38-
assert_eq!(actual, expected, "{pattern:?} disagrees with baseline: {actual:?}")
43+
assert_eq!(actual, *output, "{pattern:?} disagrees with baseline: {actual:?}")
3944
}
4045
}
4146
}
@@ -93,8 +98,7 @@ mod relative {
9398
use time::{Duration, OffsetDateTime};
9499

95100
#[test]
96-
#[should_panic] // TODO: fix
97-
fn large_offsets_can_panic() {
101+
fn large_offsets() {
98102
git_date::parse("999999999999999 weeks ago", Some(std::time::UNIX_EPOCH)).ok();
99103
}
100104

git-prompt/src/unix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ pub(crate) mod imp {
99
};
1010

1111
use nix::sys::{termios, termios::Termios};
12-
use parking_lot::{lock_api::MutexGuard, Mutex, RawMutex};
12+
use parking_lot::{const_mutex, lock_api::MutexGuard, Mutex, RawMutex};
1313

1414
use crate::{unix::TTY_PATH, Error, Mode, Options};
1515

16-
static TERM_STATE: Mutex<Option<Termios>> = Mutex::new(None);
16+
static TERM_STATE: Mutex<Option<Termios>> = const_mutex(None);
1717

1818
/// Ask the user given a `prompt`, returning the result.
1919
pub(crate) fn ask(prompt: &str, Options { mode, .. }: &Options<'_>) -> Result<String, Error> {

0 commit comments

Comments
 (0)