Skip to content

Commit

Permalink
Merge branch 'git_date_relative'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 11, 2022
2 parents e10554d + c5c6bf6 commit 83a3832
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
18 changes: 10 additions & 8 deletions git-date/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,17 @@ mod relative {

fn duration(period: &str, multiplier: i64) -> Option<Duration> {
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" => 60 * 60,
"day" => 24 * 60 * 60,
"week" => 7 * 24 * 60 * 60,
// TODO months & years? YES
// Ignore values you don't know, assume seconds then (so does git)
_ => return None,
})
};
seconds.checked_mul(multiplier).map(Duration::seconds)
}

#[cfg(test)]
Expand Down
5 changes: 0 additions & 5 deletions git-date/tests/fixtures/generate_git_date_baseline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,3 @@ baseline '123456789'
# raw
baseline '1660874655 +0800'

# failing

# empty_input
baseline ""

Git LFS file not shown
18 changes: 11 additions & 7 deletions git-date/tests/time/parse.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{collections::HashMap, str::FromStr, time::SystemTime};
use std::{collections::HashMap, time::SystemTime};

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

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

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

(|| -> Result<_> {
Expand All @@ -15,7 +15,13 @@ static BASELINE: Lazy<HashMap<BString, (usize, BString)>> = 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 = lines
.next()
.expect("three lines per baseline")
.to_str()
.expect("valid utf")
.parse()
.expect("valid epoch value");
map.insert(date_str.into(), (exit_code, output));
}
Ok(map)
Expand All @@ -34,8 +40,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:?}")
}
}
}
Expand Down Expand Up @@ -93,8 +98,7 @@ mod relative {
use time::{Duration, OffsetDateTime};

#[test]
#[should_panic] // TODO: fix
fn large_offsets_can_panic() {
fn large_offsets() {
git_date::parse("999999999999999 weeks ago", Some(std::time::UNIX_EPOCH)).ok();
}

Expand Down
4 changes: 2 additions & 2 deletions git-prompt/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Termios>> = Mutex::new(None);
static TERM_STATE: Mutex<Option<Termios>> = const_mutex(None);

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

0 comments on commit 83a3832

Please sign in to comment.