Skip to content

fix reflog parsing #1732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions gix-ref/src/store/file/log/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl<'a> From<LineRef<'a>> for Line {

///
pub mod decode {
use crate::{file::log::LineRef, parse::hex_hash};
use gix_object::bstr::{BStr, ByteSlice};
use winnow::{
combinator::{alt, eof, fail, opt, preceded, rest, terminated},
Expand All @@ -81,8 +82,6 @@ pub mod decode {
token::take_while,
};

use crate::{file::log::LineRef, parse::hex_hash};

///
mod error {
use gix_object::bstr::{BString, ByteSlice};
Expand Down Expand Up @@ -135,34 +134,57 @@ pub mod decode {
fn one<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
bytes: &mut &'a [u8],
) -> PResult<LineRef<'a>, E> {
(
(
let mut tokens = bytes.splitn(2, |b| *b == b'\t');
if let (Some(mut first), Some(mut second)) = (tokens.next(), tokens.next()) {
let (old, new, signature) = (
terminated(hex_hash, b" ").context(StrContext::Expected("<old-hexsha>".into())),
terminated(hex_hash, b" ").context(StrContext::Expected("<new-hexsha>".into())),
gix_actor::signature::decode.context(StrContext::Expected("<name> <<email>> <timestamp>".into())),
)
.context(StrContext::Expected(
"<old-hexsha> <new-hexsha> <name> <<email>> <timestamp> <tz>\\t<message>".into(),
)),
alt((
preceded(
b'\t',
message.context(StrContext::Expected("<optional message>".into())),
),
b'\n'.value(Default::default()),
eof.value(Default::default()),
fail.context(StrContext::Expected(
"log message must be separated from signature with whitespace".into(),
)),
)),
)
.map(|((old, new, signature), message)| LineRef {
))
.parse_next(&mut first)?;

// forward the buffer🤦‍♂️
message.parse_next(bytes)?;
let message = message(&mut second)?;
Ok(LineRef {
previous_oid: old,
new_oid: new,
signature,
message,
})
.parse_next(bytes)
} else {
(
(
terminated(hex_hash, b" ").context(StrContext::Expected("<old-hexsha>".into())),
terminated(hex_hash, b" ").context(StrContext::Expected("<new-hexsha>".into())),
gix_actor::signature::decode.context(StrContext::Expected("<name> <<email>> <timestamp>".into())),
)
.context(StrContext::Expected(
"<old-hexsha> <new-hexsha> <name> <<email>> <timestamp> <tz>\\t<message>".into(),
)),
alt((
preceded(
b'\t',
message.context(StrContext::Expected("<optional message>".into())),
),
b'\n'.value(Default::default()),
eof.value(Default::default()),
fail.context(StrContext::Expected(
"log message must be separated from signature with whitespace".into(),
)),
)),
)
.map(|((old, new, signature), message)| LineRef {
previous_oid: old,
new_oid: new,
signature,
message,
})
.parse_next(bytes)
}
}

#[cfg(test)]
Expand Down
19 changes: 18 additions & 1 deletion gix-ref/tests/refs/file/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ mod line {
Ok(())
}
}

mod parse {
use gix_ref::file::log;

#[test]
fn angle_bracket_in_comment() -> crate::Result {
let line = log::LineRef::from_bytes(b"7b114132d03c468a9cd97836901553658c9792de 306cdbab5457c323d1201aa8a59b3639f600a758 First Last <first.last@example.com> 1727013187 +0200\trebase (pick): Replace Into<Range<u32>> by From<LineRange>")?;
assert_eq!(line.signature.name, "First Last");
assert_eq!(line.signature.email, "first.last@example.com");
assert_eq!(line.signature.time.seconds, 1727013187);
assert_eq!(
line.message,
"rebase (pick): Replace Into<Range<u32>> by From<LineRange>"
);
Ok(())
}
}
}

mod iter {
Expand Down Expand Up @@ -204,7 +221,7 @@ mod iter {

let mut iter = gix_ref::file::log::iter::forward(log_first_broken.as_bytes());
let err = iter.next().expect("error is not none").expect_err("the line is broken");
assert_eq!(err.to_string(), "In line 1: \"134385fbroken7062102c6a483440bfda2a03 committer <committer@example.com> 946771200 +0000\\tcommit\" did not match '<old-hexsha> <new-hexsha> <name> <<email>> <timestamp> <tz>\\t<message>'");
assert_eq!(err.to_string(), "In line 1: \"0000000000000000000000000000000000000000 134385fbroken7062102c6a483440bfda2a03 committer <committer@example.com> 946771200 +0000\\tcommit\" did not match '<old-hexsha> <new-hexsha> <name> <<email>> <timestamp> <tz>\\t<message>'");
assert!(iter.next().expect("a second line").is_ok(), "line parses ok");
assert!(iter.next().is_none(), "iterator exhausted");
}
Expand Down
Loading