Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 13, 2020
1 parent dcacd3b commit 0817b24
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
27 changes: 14 additions & 13 deletions git-odb/src/pack/index/accesss.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::pack::index::{File, Kind, FAN_LEN};
use crate::pack::index::{self, FAN_LEN};
use byteorder::{BigEndian, ByteOrder};
use git_object::{self as object, SHA1_SIZE};
use std::{convert::TryInto, mem::size_of};
Expand All @@ -18,10 +18,11 @@ pub struct Entry {
pub crc32: Option<u32>,
}

impl File {
/// Iteration and access
impl index::File {
pub fn iter_v1<'a>(&'a self) -> impl Iterator<Item = Entry> + 'a {
match self.kind {
Kind::V1 => self.data[V1_HEADER_SIZE..]
index::Kind::V1 => self.data[V1_HEADER_SIZE..]
.chunks(N32_SIZE + SHA1_SIZE)
.take(self.num_objects as usize)
.map(|c| {
Expand All @@ -39,7 +40,7 @@ impl File {
pub fn iter_v2<'a>(&'a self) -> impl Iterator<Item = Entry> + 'a {
let pack64_offset = self.offset_pack_offset64_v2();
match self.kind {
Kind::V2 => izip!(
index::Kind::V2 => izip!(
self.data[V2_HEADER_SIZE..].chunks(SHA1_SIZE),
self.data[self.offset_crc32_v2()..].chunks(N32_SIZE),
self.data[self.offset_pack_offset_v2()..].chunks(N32_SIZE)
Expand All @@ -61,8 +62,8 @@ impl File {
.try_into()
.expect("an architecture able to hold 32 bits of integer");
let start = match self.kind {
Kind::V2 => V2_HEADER_SIZE + index * SHA1_SIZE,
Kind::V1 => V1_HEADER_SIZE + index * (N32_SIZE + SHA1_SIZE) + N32_SIZE,
index::Kind::V2 => V2_HEADER_SIZE + index * SHA1_SIZE,
index::Kind::V1 => V1_HEADER_SIZE + index * (N32_SIZE + SHA1_SIZE) + N32_SIZE,
};
&self.data[start..start + SHA1_SIZE]
}
Expand All @@ -72,11 +73,11 @@ impl File {
.try_into()
.expect("an architecture able to hold 32 bits of integer");
match self.kind {
Kind::V2 => {
index::Kind::V2 => {
let start = self.offset_pack_offset_v2() + index * N32_SIZE;
self.pack_offset_from_offset_v2(&self.data[start..start + N32_SIZE], self.offset_pack_offset64_v2())
}
Kind::V1 => {
index::Kind::V1 => {
let start = V1_HEADER_SIZE + index * (N32_SIZE + SHA1_SIZE);
BigEndian::read_u32(&self.data[start..start + N32_SIZE]) as u64
}
Expand All @@ -88,11 +89,11 @@ impl File {
.try_into()
.expect("an architecture able to hold 32 bits of integer");
match self.kind {
Kind::V2 => {
index::Kind::V2 => {
let start = self.offset_crc32_v2() + index * N32_SIZE;
Some(BigEndian::read_u32(&self.data[start..start + N32_SIZE]))
}
Kind::V1 => None,
index::Kind::V1 => None,
}
}

Expand Down Expand Up @@ -122,8 +123,8 @@ impl File {

pub fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = Entry> + 'a> {
match self.kind {
Kind::V2 => Box::new(self.iter_v2()),
Kind::V1 => Box::new(self.iter_v1()),
index::Kind::V2 => Box::new(self.iter_v2()),
index::Kind::V1 => Box::new(self.iter_v1()),
}
}

Expand All @@ -140,7 +141,7 @@ impl File {
}

fn pack_offset_from_offset_v2(&self, offset: &[u8], pack64_offset: usize) -> u64 {
debug_assert_eq!(self.kind, Kind::V2);
debug_assert_eq!(self.kind, index::Kind::V2);
let ofs32 = BigEndian::read_u32(offset);
if (ofs32 & N32_HIGH_BIT) == N32_HIGH_BIT {
let from = pack64_offset + (ofs32 ^ N32_HIGH_BIT) as usize * N64_SIZE;
Expand Down
10 changes: 5 additions & 5 deletions git-odb/src/pack/index/file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::pack::index::{Error, File, Kind, FAN_LEN};
use crate::pack::index::{self, Error, Kind, FAN_LEN};
use byteorder::{BigEndian, ByteOrder};
use filebuffer::FileBuffer;
use git_object::SHA1_SIZE;
Expand All @@ -9,13 +9,13 @@ const V2_SIGNATURE: &[u8] = b"\xfftOc";
const FOOTER_SIZE: usize = SHA1_SIZE * 2;

/// Instantiation
impl File {
pub fn at(path: impl AsRef<Path>) -> Result<File, Error> {
impl index::File {
pub fn at(path: impl AsRef<Path>) -> Result<index::File, Error> {
Self::try_from(path.as_ref())
}
}

impl TryFrom<&Path> for File {
impl TryFrom<&Path> for index::File {
type Error = Error;

fn try_from(path: &Path) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -54,7 +54,7 @@ impl TryFrom<&Path> for File {

(kind, version, fan, num_objects)
};
Ok(File {
Ok(index::File {
data,
kind,
num_objects,
Expand Down
9 changes: 6 additions & 3 deletions git-odb/src/pack/index/verify.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::pack::{cache, decode::DecodeEntryOutcome};
use crate::{pack, pack::index};
use crate::{
pack,
pack::index,
pack::{cache, decode::DecodeEntryOutcome},
};
use git_features::progress::{self, Progress};
use git_object::SHA1_SIZE;
use quick_error::quick_error;
Expand Down Expand Up @@ -73,7 +76,7 @@ pub struct Outcome {
pub pack_size: u64,
}

/// Methods to verify and validate the content of the index file
/// Verify and validate the content of the index file
impl index::File {
pub fn checksum_of_index(&self) -> git_object::Id {
git_object::Id::from_20_bytes(&self.data[self.data.len() - SHA1_SIZE..])
Expand Down

0 comments on commit 0817b24

Please sign in to comment.