git_ref/
parse.rs

1use git_object::bstr::{BStr, ByteSlice};
2use nom::{
3    branch::alt,
4    bytes::complete::{tag, take_while_m_n},
5    error::ParseError,
6    IResult,
7};
8
9fn is_hex_digit_lc(b: u8) -> bool {
10    matches!(b, b'0'..=b'9' | b'a'..=b'f')
11}
12
13/// Copy from https://github.com/Byron/gitoxide/blob/f270850ff92eab15258023b8e59346ec200303bd/git-object/src/immutable/parse.rs#L64
14pub fn hex_hash<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a BStr, E> {
15    // NOTE: It's important to be able to read all hashes, do not parameterize it. Hashes can be rejected at a later stage
16    // if needed.
17    take_while_m_n(
18        git_hash::Kind::shortest().len_in_hex(),
19        git_hash::Kind::longest().len_in_hex(),
20        is_hex_digit_lc,
21    )(i)
22    .map(|(i, hex)| (i, hex.as_bstr()))
23}
24
25pub fn newline<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
26    alt((tag(b"\r\n"), tag(b"\n")))(i)
27}