Skip to content

Commit

Permalink
basic midx header parsing (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Dec 20, 2021
1 parent 1a2a049 commit edf02ae
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
55 changes: 45 additions & 10 deletions git-pack/src/multi_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,30 @@ pub struct File {
data: FileBuffer,
path: std::path::PathBuf,
version: Version,
hash_kind: git_hash::Kind,
num_chunks: u8,
/// The amount of pack files contained within
num_packs: u32,
}

///
pub mod init {
pub mod access {
use crate::multi_index::File;

impl File {
pub fn num_packs(&self) -> u32 {
self.num_packs
}
pub fn hash_kind(&self) -> git_hash::Kind {
self.hash_kind
}
}
}

///
pub mod init {
use crate::multi_index::{File, Version};
use byteorder::{BigEndian, ByteOrder};
use filebuffer::FileBuffer;
use std::convert::TryFrom;
use std::path::Path;
Expand Down Expand Up @@ -58,7 +77,7 @@ pub mod init {
type Error = Error;

fn try_from(path: &Path) -> Result<Self, Self::Error> {
let data = FileBuffer::open(&path).map_err(|source| Error::Io {
let data = FileBuffer::open(path).map_err(|source| Error::Io {
source,
path: path.to_owned(),
})?;
Expand All @@ -71,28 +90,44 @@ pub mod init {
});
}

let hash_kind = {
let (version, hash_kind, num_chunks, num_packs, toc) = {
let (signature, data) = data.split_at(4);
if signature != b"MIDX" {
return Err(Error::Corrupt {
message: "Invalid signature".into(),
});
}
let (version, data) = data.split_at(1);
if version[0] != 1 {
return Err(Error::UnsupportedVersion { version: version[0] });
}
let version = match version[0] {
1 => Version::V1,
version => return Err(Error::UnsupportedVersion { version }),
};

let (hash_kind, data) = data.split_at(1);
match hash_kind[0] {
let hash_kind = match hash_kind[0] {
1 => git_hash::Kind::Sha1,
// TODO: 2 = SHA256, use it once we know it
unknown => return Err(Error::UnsupportedHashKind { kind: unknown }),
}
hash_kind
};
let (num_chunks, data) = data.split_at(1);
let num_chunks = num_chunks[0];

let (_num_base_files, data) = data.split_at(1); // TODO: handle base files once it's clear what this does

let (num_packs, toc) = data.split_at(4);
let num_packs = BigEndian::read_u32(num_packs);

(version, hash_kind, num_chunks, num_packs, toc)
};

todo!("read everything")
Ok(File {
data,
path: path.to_owned(),
version,
hash_kind,
num_chunks,
num_packs,
})
}
}
}
5 changes: 4 additions & 1 deletion git-pack/tests/pack/multi_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ fn access() {
let path = git_testtools::scripted_fixture_repo_read_only("make_pack_gen_repo_multi_index.sh")
.unwrap()
.join(".git/objects/pack/multi-pack-index");
let _file = git_pack::multi_index::File::at(path).unwrap();
let file = git_pack::multi_index::File::at(path).unwrap();

assert_eq!(file.num_packs(), 1);
assert_eq!(file.hash_kind(), git_hash::Kind::Sha1);
}

0 comments on commit edf02ae

Please sign in to comment.