Skip to content

Commit

Permalink
Merge branch 'issue-1542'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 23, 2024
2 parents f4898b9 + 1b600f3 commit c3f173c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
25 changes: 13 additions & 12 deletions gix-actor/src/signature/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub(crate) mod function {
use winnow::error::{ErrMode, ErrorKind};
use winnow::stream::Stream;
use winnow::{
combinator::{alt, separated_pair, terminated},
combinator::{alt, opt, separated_pair, terminated},
error::{AddContext, ParserError, StrContext},
prelude::*,
stream::AsChar,
Expand All @@ -21,8 +21,8 @@ pub(crate) mod function {
) -> PResult<SignatureRef<'a>, E> {
separated_pair(
identity,
b" ",
(
opt(b" "),
opt((
terminated(take_until(0.., SPACE), take(1usize))
.verify_map(|v| to_signed::<SecondsSinceUnixEpoch>(v).ok())
.context(StrContext::Expected("<timestamp>".into())),
Expand All @@ -38,8 +38,9 @@ pub(crate) mod function {
.verify_map(|v| to_signed::<OffsetInSeconds>(v).ok())
.context(StrContext::Expected("MM".into())),
take_while(0.., AsChar::is_dec_digit).map(|v: &[u8]| v),
)
.map(|(time, sign, hours, minutes, trailing_digits)| {
))
.map(|maybe_timestamp| {
if let Some((time, sign, hours, minutes, trailing_digits)) = maybe_timestamp {
let offset = if trailing_digits.is_empty() {
(hours * 3600 + minutes * 60) * if sign == Sign::Minus { -1 } else { 1 }
} else {
Expand All @@ -50,7 +51,10 @@ pub(crate) mod function {
offset,
sign,
}
}),
} else {
Time::new(0, 0)
}
}),
)
.context(StrContext::Expected("<name> <<email>> <timestamp> <+|-><HHMM>".into()))
.map(|(identity, time)| SignatureRef {
Expand Down Expand Up @@ -206,12 +210,9 @@ mod tests {
#[test]
fn invalid_time() {
assert_eq!(
decode.parse_peek(b"hello <> abc -1215")
.map_err(to_bstr_err)
.expect_err("parse fails as > is missing")
.to_string(),
"in predicate verification at 'abc -1215'\n 0: expected `<timestamp>` at 'abc -1215'\n 1: expected `<name> <<email>> <timestamp> <+|-><HHMM>` at 'hello <> abc -1215'\n"
);
decode.parse_peek(b"hello <> abc -1215").expect("parse to work").1,
signature("hello", "", 0, Sign::Plus, 0)
);
}
}
}
14 changes: 14 additions & 0 deletions gix-actor/tests/signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,17 @@ fn parse_timestamp_with_trailing_digits() {
}
);
}

#[test]
fn parse_missing_timestamp() {
let signature = gix_actor::SignatureRef::from_bytes::<()>(b"first last <name@example.com>")
.expect("deal with missing timestamp in signature by zeroing it");
assert_eq!(
signature,
SignatureRef {
name: "first last".into(),
email: "name@example.com".into(),
time: gix_actor::date::Time::new(0, 0),
}
);
}
2 changes: 1 addition & 1 deletion gix-object/tests/commit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn invalid() {
assert_eq!(
CommitRef::from_bytes(partial_commit).unwrap_err().to_string(),
if cfg!(feature = "verbose-object-parsing-errors") {
"object parsing failed at `1`\nexpected `<timestamp>`, `<name> <<email>> <timestamp> <+|-><HHMM>`, `author <signature>`"
"object parsing failed at `1`\nexpected `author <signature>`"
} else {
"object parsing failed"
}
Expand Down
4 changes: 4 additions & 0 deletions gix-object/tests/fixtures/tag/tagger-without-timestamp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object 4fcd840c4935e4c7a5ea3552710a0f26b9178c24
type commit
tag ChangeLog
tagger shemminger <shemminger>
28 changes: 27 additions & 1 deletion gix-object/tests/tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ fn invalid() {
}

mod from_bytes {
use gix_actor::SignatureRef;
use gix_date::Time;
use gix_object::{bstr::ByteSlice, Kind, TagRef};

use crate::{fixture_name, signature, tag::tag_fixture};
use crate::{fixture_name, signature, tag::tag_fixture, Sign};

#[test]
fn signed() -> crate::Result {
Expand Down Expand Up @@ -229,6 +231,30 @@ KLMHist5yj0sw1E4hDTyQa0=
);
Ok(())
}

#[test]
fn tagger_without_timestamp() -> crate::Result {
assert_eq!(
TagRef::from_bytes(&fixture_name("tag", "tagger-without-timestamp.txt"))?,
TagRef {
target: b"4fcd840c4935e4c7a5ea3552710a0f26b9178c24".as_bstr(),
name: b"ChangeLog".as_bstr(),
target_kind: Kind::Commit,
message: b"".as_bstr(),
tagger: Some(SignatureRef {
name: b"shemminger".as_bstr(),
email: b"shemminger".as_bstr(),
time: Time {
seconds: 0,
offset: 0,
sign: Sign::Plus
}
}),
pgp_signature: None
}
);
Ok(())
}
}

fn tag_fixture(offset: i32) -> TagRef<'static> {
Expand Down
4 changes: 2 additions & 2 deletions gix-object/tests/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ mod entries {
)
}

assert_eq!(
failures_when_searching_by_name, 2,
assert_ne!(
failures_when_searching_by_name, 0,
"it's not possible to do a binary search by name alone"
);

Expand Down

0 comments on commit c3f173c

Please sign in to comment.