Skip to content

Commit

Permalink
remove byteorder dependency from git-commitgraph (#293)
Browse files Browse the repository at this point in the history
This is now sufficiently well implemented in the standard library.
  • Loading branch information
Byron committed Jan 9, 2022
1 parent 826ca0c commit c526811
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion git-commitgraph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ git-hash = { version ="^0.8.0", path = "../git-hash" }
git-chunk = { version ="^0.2.0", path = "../git-chunk" }

bstr = { version = "0.2.13", default-features = false, features = ["std"] }
byteorder = "1.2.3"
memmap2 = "0.5.0"
serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"] }
thiserror = "1.0.26"
Expand Down
18 changes: 11 additions & 7 deletions git-commitgraph/src/file/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use std::{
slice::Chunks,
};

use byteorder::{BigEndian, ByteOrder};

use crate::{
file::{self, File, EXTENDED_EDGES_MASK, LAST_EXTENDED_EDGE_MASK, NO_PARENT},
graph,
Expand Down Expand Up @@ -38,17 +36,23 @@ pub struct Commit<'a> {
root_tree_id: &'a git_hash::oid,
}

#[inline]
fn read_u32(b: &[u8]) -> u32 {
u32::from_be_bytes(b.try_into().unwrap())
}

impl<'a> Commit<'a> {
pub(crate) fn new(file: &'a File, pos: file::Position) -> Self {
let bytes = file.commit_data_bytes(pos);
Commit {
file,
pos,
root_tree_id: git_hash::oid::from_bytes_unchecked(&bytes[..file.hash_len]),
parent1: ParentEdge::from_raw(BigEndian::read_u32(&bytes[file.hash_len..][..4])),
parent2: ParentEdge::from_raw(BigEndian::read_u32(&bytes[file.hash_len + 4..][..4])),
generation: BigEndian::read_u32(&bytes[file.hash_len + 8..][..4]) >> 2,
commit_timestamp: BigEndian::read_u64(&bytes[file.hash_len + 8..][..8]) & 0x0003_ffff_ffff,
parent1: ParentEdge::from_raw(read_u32(&bytes[file.hash_len..][..4])),
parent2: ParentEdge::from_raw(read_u32(&bytes[file.hash_len + 4..][..4])),
generation: read_u32(&bytes[file.hash_len + 8..][..4]) >> 2,
commit_timestamp: u64::from_be_bytes(bytes[file.hash_len + 8..][..8].try_into().unwrap())
& 0x0003_ffff_ffff,
}
}

Expand Down Expand Up @@ -173,7 +177,7 @@ impl<'a> Iterator for ParentIterator<'a> {
},
ParentIteratorState::Extra(mut chunks) => {
if let Some(chunk) = chunks.next() {
let extra_edge = BigEndian::read_u32(chunk);
let extra_edge = read_u32(chunk);
match ExtraEdge::from_raw(extra_edge) {
ExtraEdge::Internal(pos) => {
self.state = ParentIteratorState::Extra(chunks);
Expand Down
3 changes: 1 addition & 2 deletions git-commitgraph/src/file/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{
};

use bstr::ByteSlice;
use byteorder::{BigEndian, ByteOrder};
use memmap2::Mmap;

use crate::file::{
Expand Down Expand Up @@ -249,7 +248,7 @@ impl TryFrom<&Path> for File {
fn read_fan(d: &[u8]) -> ([u32; FAN_LEN], usize) {
let mut fan = [0; FAN_LEN];
for (c, f) in d.chunks(4).zip(fan.iter_mut()) {
*f = BigEndian::read_u32(c);
*f = u32::from_be_bytes(c.try_into().unwrap());
}
(fan, FAN_LEN * 4)
}

0 comments on commit c526811

Please sign in to comment.