Skip to content

Commit 9fdd34b

Browse files
committed
refactor (#293)
1 parent cc33752 commit 9fdd34b

File tree

3 files changed

+43
-58
lines changed

3 files changed

+43
-58
lines changed

Diff for: git-index/src/extension.rs

+41-56
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,58 @@ fn decode_header(data: &[u8]) -> (Signature, u32, &[u8]) {
1010
(signature.try_into().unwrap(), read_u32(size), data)
1111
}
1212

13-
mod end_of_index_entry {
14-
use crate::{extension, extension::EndOfIndexEntry, file::header, util::read_u32};
13+
pub(crate) mod end_of_index_entry {
14+
use crate::{extension, extension::Signature, file::header, util::read_u32};
1515

16-
impl EndOfIndexEntry {
17-
pub fn from_bytes(data: &[u8], object_hash: git_hash::Kind) -> Option<Self> {
18-
let hash_len = object_hash.len_in_bytes();
19-
if data.len() < EndOfIndexEntry::SIZE_WITH_HEADER + hash_len {
20-
return None;
21-
}
16+
pub const SIGNATURE: Signature = *b"EOIE";
17+
pub const SIZE: usize = 4 /* offset to extensions */ + git_hash::Kind::Sha1.len_in_bytes();
18+
pub const SIZE_WITH_HEADER: usize = crate::extension::MIN_SIZE + SIZE;
2219

23-
let start_of_eoie = data.len() - EndOfIndexEntry::SIZE_WITH_HEADER - hash_len;
24-
let ext_data = &data[start_of_eoie..data.len() - hash_len];
20+
pub fn decode(data: &[u8], object_hash: git_hash::Kind) -> Option<usize> {
21+
let hash_len = object_hash.len_in_bytes();
22+
if data.len() < SIZE_WITH_HEADER + hash_len {
23+
return None;
24+
}
2525

26-
let (signature, ext_size, ext_data) = extension::decode_header(ext_data);
27-
if signature != EndOfIndexEntry::SIGNATURE || ext_size as usize != EndOfIndexEntry::SIZE {
28-
return None;
29-
}
26+
let start_of_eoie = data.len() - SIZE_WITH_HEADER - hash_len;
27+
let ext_data = &data[start_of_eoie..data.len() - hash_len];
3028

31-
let (offset, checksum) = ext_data.split_at(4);
32-
let offset = read_u32(offset) as usize;
33-
if offset < header::SIZE || offset > start_of_eoie || checksum.len() != git_hash::Kind::Sha1.len_in_bytes()
34-
{
35-
return None;
36-
}
29+
let (signature, ext_size, ext_data) = extension::decode_header(ext_data);
30+
if signature != SIGNATURE || ext_size as usize != SIZE {
31+
return None;
32+
}
3733

38-
let mut hasher = git_features::hash::hasher(git_hash::Kind::Sha1);
39-
let mut last_chunk = None;
40-
for (signature, chunk) in
41-
extension::Iter::new(&data[offset..data.len() - EndOfIndexEntry::SIZE_WITH_HEADER - hash_len])
42-
{
43-
hasher.update(&signature);
44-
hasher.update(&(chunk.len() as u32).to_be_bytes());
45-
last_chunk = Some(chunk);
46-
}
34+
let (offset, checksum) = ext_data.split_at(4);
35+
let offset = read_u32(offset) as usize;
36+
if offset < header::SIZE || offset > start_of_eoie || checksum.len() != git_hash::Kind::Sha1.len_in_bytes() {
37+
return None;
38+
}
4739

48-
if hasher.digest() != checksum {
49-
return None;
50-
}
51-
// The last-to-this chunk ends where ours starts
52-
if last_chunk
53-
.map(|s| s.as_ptr_range().end != (&data[start_of_eoie]) as *const _)
54-
.unwrap_or(true)
55-
{
56-
return None;
57-
}
58-
todo!("euio")
40+
let mut hasher = git_features::hash::hasher(git_hash::Kind::Sha1);
41+
let mut last_chunk = None;
42+
for (signature, chunk) in extension::Iter::new(&data[offset..data.len() - SIZE_WITH_HEADER - hash_len]) {
43+
hasher.update(&signature);
44+
hasher.update(&(chunk.len() as u32).to_be_bytes());
45+
last_chunk = Some(chunk);
5946
}
47+
48+
if hasher.digest() != checksum {
49+
return None;
50+
}
51+
// The last-to-this chunk ends where ours starts
52+
if last_chunk
53+
.map(|s| s.as_ptr_range().end != (&data[start_of_eoie]) as *const _)
54+
.unwrap_or(true)
55+
{
56+
return None;
57+
}
58+
59+
Some(offset)
6060
}
6161
}
6262

6363
mod iter {
64-
use crate::extension;
65-
use crate::extension::Iter;
66-
use crate::util::read_u32;
64+
use crate::{extension, extension::Iter, util::read_u32};
6765

6866
impl<'a> Iter<'a> {
6967
pub fn new(data_at_beginning_of_extensions_and_truncated: &'a [u8]) -> Self {
@@ -102,16 +100,3 @@ mod iter {
102100
pub struct Iter<'a> {
103101
data: &'a [u8],
104102
}
105-
106-
pub struct EndOfIndexEntry {
107-
/// The offset the the beginning of all extensions, or the end of all entries.
108-
offset_to_extensions: u32,
109-
/// The SHA1 checksum over the signature and size of all extensions.
110-
checksum: git_hash::ObjectId,
111-
}
112-
113-
impl EndOfIndexEntry {
114-
pub const SIGNATURE: Signature = *b"EOIE";
115-
pub const SIZE: usize = 4 /* offset to extensions */ + git_hash::Kind::Sha1.len_in_bytes();
116-
pub const SIZE_WITH_HEADER: usize = crate::extension::MIN_SIZE + Self::SIZE;
117-
}

Diff for: git-index/src/file.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub mod init {
4242
};
4343

4444
let (version, num_entries, post_header_data) = header::decode(&data, object_hash)?;
45-
let start_of_extensions = extension::EndOfIndexEntry::from_bytes(&data, object_hash);
45+
let start_of_extensions = extension::end_of_index_entry::decode(&data, object_hash);
4646

4747
Ok(File {
4848
state: State { timestamp: mtime },

Diff for: git-index/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use filetime::FileTime;
77

88
pub mod file;
99

10-
pub mod extension;
10+
pub(crate) mod extension;
1111

1212
pub mod init {
1313
use filetime::FileTime;

0 commit comments

Comments
 (0)