Skip to content

Commit

Permalink
Allow for more screen space when formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 1, 2020
1 parent 74bcbb5 commit 6794300
Show file tree
Hide file tree
Showing 25 changed files with 145 additions and 400 deletions.
9 changes: 1 addition & 8 deletions demos/git-count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,7 @@ fn run() -> Result<()> {
.par_iter()
.fold(
|| (0, 0, 0, 0, 0),
|(mut deltas, mut commits, mut trees, mut blobs, mut tags): (
u32,
u32,
u32,
u32,
u32,
),
entry| {
|(mut deltas, mut commits, mut trees, mut blobs, mut tags): (u32, u32, u32, u32, u32), entry| {
match pack.entry(entry.pack_offset).header {
Commit => commits += 1,
Tag => tags += 1,
Expand Down
10 changes: 2 additions & 8 deletions git-features/src/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ mod serial {
use crate::parallel::Reducer;

#[cfg(not(feature = "parallel"))]
pub fn join<O1: Send, O2: Send>(
left: impl FnOnce() -> O1 + Send,
right: impl FnOnce() -> O2 + Send,
) -> (O1, O2) {
pub fn join<O1: Send, O2: Send>(left: impl FnOnce() -> O1 + Send, right: impl FnOnce() -> O2 + Send) -> (O1, O2) {
(left(), right())
}

Expand Down Expand Up @@ -41,10 +38,7 @@ mod in_parallel {
use crate::parallel::Reducer;
use crossbeam_utils::thread;

pub fn join<O1: Send, O2: Send>(
left: impl FnOnce() -> O1 + Send,
right: impl FnOnce() -> O2 + Send,
) -> (O1, O2) {
pub fn join<O1: Send, O2: Send>(left: impl FnOnce() -> O1 + Send, right: impl FnOnce() -> O2 + Send) -> (O1, O2) {
thread::scope(|s| {
let left = s.spawn(|_| left());
let right = s.spawn(|_| right());
Expand Down
4 changes: 1 addition & 3 deletions git-features/src/progress/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ impl Progress for Log {
{
self.last_set = Some(now);
match (self.max, self.unit) {
(Some(max), Some(unit)) => {
log::info!("{} → {} / {} {}", self.name, step, max, unit)
}
(Some(max), Some(unit)) => log::info!("{} → {} / {} {}", self.name, step, max, unit),
(None, Some(unit)) => log::info!("{} → {} {}", self.name, step, unit),
(Some(max), None) => log::info!("{} → {} / {}", self.name, step, max),
(None, None) => log::info!("{} → {}", self.name, step),
Expand Down
30 changes: 12 additions & 18 deletions git-object/src/borrowed/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,24 @@ pub struct Commit<'data> {
pub fn parse_message(i: &[u8]) -> IResult<&[u8], &BStr, Error> {
if i.is_empty() {
// newline + [message]
return Err(nom::Err::Error(Error::NomDetail(
i.into(),
"commit message is missing",
)));
return Err(nom::Err::Error(Error::NomDetail(i.into(), "commit message is missing")));
}
let (i, _) = tag(NL)(i).map_err(Error::context(
"a newline separates headers from the message",
))?;
let (i, _) = tag(NL)(i).map_err(Error::context("a newline separates headers from the message"))?;
debug_assert!(!i.is_empty());
Ok((&[], &i.as_bstr()))
}

pub fn parse(i: &[u8]) -> IResult<&[u8], Commit, Error> {
let (i, tree) = parse_header_field(i, b"tree", parse_hex_sha1)
.map_err(Error::context("tree <40 lowercase hex char>"))?;
let (i, parents) = many0(|i| parse_header_field(i, b"parent", parse_hex_sha1))(i).map_err(
Error::context("zero or more 'parent <40 lowercase hex char>'"),
)?;
let (i, author) = parse_header_field(i, b"author", parse_signature)
.map_err(Error::context("author <signature>"))?;
let (i, committer) = parse_header_field(i, b"committer", parse_signature)
.map_err(Error::context("author <signature>"))?;
let (i, encoding) = opt(|i| parse_header_field(i, b"encoding", is_not(NL)))(i)
.map_err(Error::context("author <signature>"))?;
let (i, tree) =
parse_header_field(i, b"tree", parse_hex_sha1).map_err(Error::context("tree <40 lowercase hex char>"))?;
let (i, parents) = many0(|i| parse_header_field(i, b"parent", parse_hex_sha1))(i)
.map_err(Error::context("zero or more 'parent <40 lowercase hex char>'"))?;
let (i, author) =
parse_header_field(i, b"author", parse_signature).map_err(Error::context("author <signature>"))?;
let (i, committer) =
parse_header_field(i, b"committer", parse_signature).map_err(Error::context("author <signature>"))?;
let (i, encoding) =
opt(|i| parse_header_field(i, b"encoding", is_not(NL)))(i).map_err(Error::context("author <signature>"))?;
let (i, pgp_signature) = opt(alt((
|i| parse_header_field_multiline(i, b"gpgsig"),
|i| parse_header_field(i, b"gpgsig", is_not(NL)),
Expand Down
24 changes: 10 additions & 14 deletions git-object/src/borrowed/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ pub struct Tag<'data> {
}

fn parse(i: &[u8]) -> IResult<&[u8], Tag, Error> {
let (i, target) = parse_header_field(i, b"object", parse_hex_sha1)
.map_err(Error::context("object <40 lowercase hex char>"))?;
let (i, target) =
parse_header_field(i, b"object", parse_hex_sha1).map_err(Error::context("object <40 lowercase hex char>"))?;

let (i, kind) = parse_header_field(i, b"type", take_while1(is_alphabetic))
.map_err(Error::context("type <object kind>"))?;
let kind =
crate::Kind::from_bytes(kind).map_err(|e| nom::Err::Error(Error::ParseKindError(e)))?;
let (i, kind) =
parse_header_field(i, b"type", take_while1(is_alphabetic)).map_err(Error::context("type <object kind>"))?;
let kind = crate::Kind::from_bytes(kind).map_err(|e| nom::Err::Error(Error::ParseKindError(e)))?;

let (i, tag_version) = parse_header_field(i, b"tag", take_while1(|b| b != NL[0]))
.map_err(Error::context("tag <version>"))?;
let (i, tag_version) =
parse_header_field(i, b"tag", take_while1(|b| b != NL[0])).map_err(Error::context("tag <version>"))?;

let (i, signature) = parse_header_field(i, b"tagger", parse_signature)
.map_err(Error::context("tagger <signature>"))?;
let (i, signature) =
parse_header_field(i, b"tagger", parse_signature).map_err(Error::context("tagger <signature>"))?;
let (i, (message, pgp_signature)) = all_consuming(parse_message)(i)?;
Ok((
i,
Expand All @@ -65,10 +64,7 @@ fn parse_message(i: &[u8]) -> IResult<&[u8], (&BStr, Option<&BStr>), Error> {
let (i, _) = tag(NL)(i)?;
fn all_to_end(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8]), Error> {
if i.is_empty() {
return Err(nom::Err::Error(Error::NomDetail(
i.into(),
"tag message is missing",
)));
return Err(nom::Err::Error(Error::NomDetail(i.into(), "tag message is missing")));
}
// an empty signature message signals that there is none - the function signature is needed
// to work with 'alt(…)'. PGP signatures are never empty
Expand Down
34 changes: 7 additions & 27 deletions git-object/src/borrowed/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use nom::{
pub(crate) const NL: &[u8] = b"\n";
pub(crate) const SPACE: &[u8] = b" ";

pub(crate) fn parse_header_field_multiline<'a>(
i: &'a [u8],
name: &'static [u8],
) -> IResult<&'a [u8], &'a [u8], Error> {
pub(crate) fn parse_header_field_multiline<'a>(i: &'a [u8], name: &'static [u8]) -> IResult<&'a [u8], &'a [u8], Error> {
let (i, o) = peek(preceded(
terminated(tag(name), tag(SPACE)),
recognize(tuple((
Expand All @@ -38,10 +35,7 @@ pub(crate) fn parse_header_field<'a, T>(
name: &'static [u8],
parse_value: impl Fn(&'a [u8]) -> IResult<&'a [u8], T, Error>,
) -> IResult<&'a [u8], T, Error> {
terminated(
preceded(terminated(tag(name), tag(SPACE)), parse_value),
tag(NL),
)(i)
terminated(preceded(terminated(tag(name), tag(SPACE)), parse_value), tag(NL))(i)
}

fn is_hex_digit_lc(b: u8) -> bool {
Expand Down Expand Up @@ -69,25 +63,11 @@ pub(crate) fn parse_signature(i: &[u8]) -> IResult<&[u8], Signature, Error> {
"tagger <name> <<email>> <time seconds since epoch> <+|-><HHMM>",
))?;

let sign = if tzsign[0] == b'-' {
Sign::Minus
} else {
Sign::Plus
};
let hours = btoi::<i32>(&tzhour).map_err(|e| {
nom::Err::Error(Error::ParseIntegerError(
"invalid 'hours' string",
tzhour.into(),
e,
))
})?;
let minutes = btoi::<i32>(&tzminute).map_err(|e| {
nom::Err::Error(Error::ParseIntegerError(
"invalid 'minutes' string",
tzminute.into(),
e,
))
})?;
let sign = if tzsign[0] == b'-' { Sign::Minus } else { Sign::Plus };
let hours = btoi::<i32>(&tzhour)
.map_err(|e| nom::Err::Error(Error::ParseIntegerError("invalid 'hours' string", tzhour.into(), e)))?;
let minutes = btoi::<i32>(&tzminute)
.map_err(|e| nom::Err::Error(Error::ParseIntegerError("invalid 'minutes' string", tzminute.into(), e)))?;
let offset = (hours * 3600 + minutes * 60) * if sign == Sign::Minus { -1 } else { 1 };

Ok((
Expand Down
32 changes: 4 additions & 28 deletions git-object/src/borrowed/util/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ mod parse_signature {
use crate::{Sign, Time};
use bstr::ByteSlice;

fn signature(
name: &'static str,
email: &'static str,
time: u32,
sign: Sign,
offset: i32,
) -> Signature<'static> {
fn signature(name: &'static str, email: &'static str, time: u32, sign: Sign, offset: i32) -> Signature<'static> {
Signature {
name: name.as_bytes().as_bstr(),
email: email.as_bytes().as_bstr(),
Expand All @@ -24,13 +18,7 @@ mod parse_signature {
parse_signature(b"Sebastian Thiel <byronimo@gmail.com> 1528473343 -0230")
.unwrap()
.1,
signature(
"Sebastian Thiel",
"byronimo@gmail.com",
1528473343,
Sign::Minus,
-9000
)
signature("Sebastian Thiel", "byronimo@gmail.com", 1528473343, Sign::Minus, -9000)
);
}

Expand All @@ -40,13 +28,7 @@ mod parse_signature {
parse_signature(b"Sebastian Thiel <byronimo@gmail.com> 1528473343 +0230")
.unwrap()
.1,
signature(
"Sebastian Thiel",
"byronimo@gmail.com",
1528473343,
Sign::Plus,
9000
)
signature("Sebastian Thiel", "byronimo@gmail.com", 1528473343, Sign::Plus, 9000)
);
}

Expand All @@ -56,13 +38,7 @@ mod parse_signature {
parse_signature(b"Sebastian Thiel <byronimo@gmail.com> 1528473343 -0000")
.unwrap()
.1,
signature(
"Sebastian Thiel",
"byronimo@gmail.com",
1528473343,
Sign::Minus,
0
)
signature("Sebastian Thiel", "byronimo@gmail.com", 1528473343, Sign::Minus, 0)
);
}

Expand Down
17 changes: 4 additions & 13 deletions git-object/tests/borrowed/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ mod method {
fn tree() {
let fixture = fixture_bytes("commit", "unsigned.txt");
let commit = Commit::from_bytes(&fixture).unwrap();
assert_eq!(
commit.tree(),
hex_to_id("1b2dfb4ac5e42080b682fc676e9738c94ce6d54d")
);
assert_eq!(commit.tree(), hex_to_id("1b2dfb4ac5e42080b682fc676e9738c94ce6d54d"));
assert_eq!(commit.tree, "1b2dfb4ac5e42080b682fc676e9738c94ce6d54d");
}
}
Expand Down Expand Up @@ -43,9 +40,7 @@ mod from_bytes {
Commit::from_bytes(&fixture_bytes("commit", "whitespace.txt")).unwrap(),
Commit {
tree: b"9bed6275068a0575243ba8409253e61af81ab2ff".as_bstr(),
parents: SmallVec::from(
vec![b"26b4df046d1776c123ac69d918f5aec247b58cc6".as_bstr()]
),
parents: SmallVec::from(vec![b"26b4df046d1776c123ac69d918f5aec247b58cc6".as_bstr()]),
author: signature(1592448450),
committer: signature(1592448450),
encoding: None,
Expand All @@ -61,9 +56,7 @@ mod from_bytes {
Commit::from_bytes(&fixture_bytes("commit", "signed-singleline.txt")).unwrap(),
Commit {
tree: b"00fc39317701176e326974ce44f5bd545a32ec0b".as_bstr(),
parents: SmallVec::from(
vec![b"09d8d3a12e161a7f6afb522dbe8900a9c09bce06".as_bstr()]
),
parents: SmallVec::from(vec![b"09d8d3a12e161a7f6afb522dbe8900a9c09bce06".as_bstr()]),
author: signature(1592391367),
committer: signature(1592391367),
encoding: None,
Expand Down Expand Up @@ -111,9 +104,7 @@ mod from_bytes {
Commit::from_bytes(&fixture_bytes("commit", "with-encoding.txt")).unwrap(),
Commit {
tree: b"4a1c03029e7407c0afe9fc0320b3258e188b115e".as_bstr(),
parents: SmallVec::from(
vec![b"7ca98aad461a5c302cb4c9e3acaaa6053cc67a62".as_bstr()]
),
parents: SmallVec::from(vec![b"7ca98aad461a5c302cb4c9e3acaaa6053cc67a62".as_bstr()]),
author: signature(1592438199),
committer: signature(1592438199),
encoding: Some("ISO-8859-1".into()),
Expand Down
5 changes: 1 addition & 4 deletions git-object/tests/borrowed/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ mod method {
fn target() {
let fixture = fixture_bytes("tag", "signed.txt");
let tag = Tag::from_bytes(&fixture).unwrap();
assert_eq!(
tag.target(),
hex_to_id("ffa700b4aca13b80cb6b98a078e7c96804f8e0ec")
);
assert_eq!(tag.target(), hex_to_id("ffa700b4aca13b80cb6b98a078e7c96804f8e0ec"));
assert_eq!(tag.target, "ffa700b4aca13b80cb6b98a078e7c96804f8e0ec")
}
}
Expand Down
7 changes: 2 additions & 5 deletions git-odb/src/loose/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ impl Db {
let mut is_valid_path = false;
let e = res.map_err(Error::WalkDir).map(|e| {
let p = e.path();
let (c1, c2) = p
.components()
.fold((None, None), |(_c1, c2), cn| (c2, Some(cn)));
let (c1, c2) = p.components().fold((None, None), |(_c1, c2), cn| (c2, Some(cn)));
if let (Some(Normal(c1)), Some(Normal(c2))) = (c1, c2) {
if c1.len() == 2 && c2.len() == 38 {
if let (Some(c1), Some(c2)) = (c1.to_str(), c2.to_str()) {
Expand Down Expand Up @@ -106,8 +104,7 @@ impl Db {
let mut decompressed = [0; HEADER_READ_UNCOMPRESSED_BYTES];
let mut compressed = [0; HEADER_READ_COMPRESSED_BYTES];
let ((_status, _consumed_in, consumed_out), bytes_read, mut input_stream) = {
let mut istream =
fs::File::open(&path).map_err(|e| Error::Io(e, "open", path.to_owned()))?;
let mut istream = fs::File::open(&path).map_err(|e| Error::Io(e, "open", path.to_owned()))?;
let bytes_read = istream
.read(&mut compressed[..])
.map_err(|e| Error::Io(e, "read", path.to_owned()))?;
Expand Down
15 changes: 3 additions & 12 deletions git-odb/src/loose/db/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ pub fn parse_header(input: &[u8]) -> Result<(object::Kind, usize, usize), Error>
(Some(kind), Some(size)) => Ok((
object::Kind::from_bytes(kind)?,
btoi::btoi(size).map_err(|e| {
Error::ParseIntegerError(
"Object size in header could not be parsed",
size.to_owned(),
e,
)
Error::ParseIntegerError("Object size in header could not be parsed", size.to_owned(), e)
})?,
header_end + 1, // account for 0 byte
)),
Expand All @@ -35,11 +31,7 @@ fn kind_to_bytes_with_space(object: object::Kind) -> &'static [u8] {
}
}

pub fn write_header(
object: object::Kind,
size: usize,
mut out: impl std::io::Write,
) -> Result<usize, std::io::Error> {
pub fn write_header(object: object::Kind, size: usize, mut out: impl std::io::Write) -> Result<usize, std::io::Error> {
let mut written = out.write(kind_to_bytes_with_space(object))?;
written += itoa::write(&mut out, size)?;
out.write_u8(0)?;
Expand All @@ -63,8 +55,7 @@ mod tests {
] {
let written = write_header(*kind, *size, &mut buf[..]).unwrap();
assert_eq!(buf[..written].as_bstr(), expected.as_bstr());
let (actual_kind, actual_size, actual_read) =
parse_header(&buf[..written]).unwrap();
let (actual_kind, actual_size, actual_read) = parse_header(&buf[..written]).unwrap();
assert_eq!(actual_kind, *kind);
assert_eq!(actual_size, *size);
assert_eq!(actual_read, written);
Expand Down
9 changes: 3 additions & 6 deletions git-odb/src/loose/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,16 @@ impl Object {
if let Some(path) = self.path.take() {
// NOTE: For now we just re-read everything from the beginning without seeking, as our buffer
// is small so the seek might be more expensive than just reading everything.
let mut file =
std::fs::File::open(&path).map_err(|e| Error::Io(e, "open", path.clone()))?;
let mut file = std::fs::File::open(&path).map_err(|e| Error::Io(e, "open", path.clone()))?;
let file_size = file
.metadata()
.map_err(|e| Error::Io(e, "read metadata", path.clone()))?
.len() as usize;
let mut buf = Vec::with_capacity(file_size);
file.read_to_end(&mut buf)
.map_err(|e| Error::Io(e, "read", path))?;
file.read_to_end(&mut buf).map_err(|e| Error::Io(e, "read", path))?;
self.compressed_data = SmallVec::from(buf);
}
self.decompressed_data =
SmallVec::from(decompress_to_vec_zlib(&self.compressed_data[..]).unwrap());
self.decompressed_data = SmallVec::from(decompress_to_vec_zlib(&self.compressed_data[..]).unwrap());
self.decompressed_data.shrink_to_fit();
assert!(self.decompressed_data.len() == total_size);
self.decompression_complete = true;
Expand Down
Loading

0 comments on commit 6794300

Please sign in to comment.