-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use bstr::{BStr, ByteSlice}; | ||
use nom::{ | ||
branch::alt, | ||
bytes::complete::{tag, take_while_m_n}, | ||
error::ParseError, | ||
IResult, | ||
}; | ||
|
||
fn is_hex_digit_lc(b: u8) -> bool { | ||
matches!(b, b'0'..=b'9' | b'a'..=b'f') | ||
} | ||
|
||
/// Copy from https://github.com/Byron/gitoxide/blob/f270850ff92eab15258023b8e59346ec200303bd/git-object/src/immutable/parse.rs#L64 | ||
pub fn hex_hash<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a BStr, E> { | ||
take_while_m_n( | ||
git_hash::Kind::shortest().len_in_hex(), | ||
git_hash::Kind::longest().len_in_hex(), | ||
is_hex_digit_lc, | ||
)(i) | ||
.map(|(i, hex)| (i, hex.as_bstr())) | ||
} | ||
|
||
pub fn newline<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> { | ||
alt((tag(b"\r\n"), tag(b"\n")))(i) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::{Kind, Target}; | ||
use bstr::BStr; | ||
use git_hash::oid; | ||
|
||
impl<'a> Target<'a> { | ||
/// Returns the kind of the target the ref is pointing to. | ||
pub fn kind(&self) -> Kind { | ||
match self { | ||
Target::Symbolic(_) => Kind::Symbolic, | ||
Target::Peeled(_) => Kind::Peeled, | ||
} | ||
} | ||
/// Interpret this target as object id which maybe `None` if it is symbolic. | ||
pub fn as_id(&self) -> Option<&oid> { | ||
match self { | ||
Target::Symbolic(_) => None, | ||
Target::Peeled(oid) => Some(oid), | ||
} | ||
} | ||
/// Interpret this target as name of the reference it points to which maybe `None` if it an object id. | ||
pub fn as_name(&self) -> Option<&BStr> { | ||
match self { | ||
Target::Symbolic(path) => Some(path), | ||
Target::Peeled(_) => None, | ||
} | ||
} | ||
/// Convert this instance into an owned version, without consuming it. | ||
pub fn to_owned(self) -> crate::mutable::Target { | ||
self.into() | ||
} | ||
} |