Skip to content

Commit 79ca582

Browse files
committed
right before implementing a traversal over extension chunks (#293)
1 parent 9b28b18 commit 79ca582

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
lines changed

git-index/src/extension.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use crate::{util::read_u32, Version};
2+
3+
const MIN_SIZE: usize = 4 /* signature */ + 4 /* size */;
4+
5+
fn decode_header(data: &[u8]) -> ([u8; 4], u32, &[u8]) {
6+
let (signature, data) = data.split_at(4);
7+
let (size, data) = data.split_at(4);
8+
(signature.try_into().unwrap(), read_u32(size), data)
9+
}
10+
11+
mod end_of_index_entry {
12+
use crate::{extension, extension::EndOfIndexEntry, file::header, util::read_u32};
13+
14+
impl EndOfIndexEntry {
15+
pub fn from_bytes(data: &[u8], object_hash: git_hash::Kind) -> Option<Self> {
16+
let hash_len = object_hash.len_in_bytes();
17+
if data.len() < EndOfIndexEntry::SIZE_WITH_HEADER + hash_len {
18+
return None;
19+
}
20+
21+
let start_of_eoie = data.len() - EndOfIndexEntry::SIZE_WITH_HEADER - hash_len;
22+
let data = &data[start_of_eoie..][..hash_len];
23+
24+
let (signature, ext_size, data) = extension::decode_header(data);
25+
if &signature != EndOfIndexEntry::SIGNATURE || ext_size as usize != EndOfIndexEntry::SIZE {
26+
return None;
27+
}
28+
29+
let (offset, hash) = data.split_at(4);
30+
let offset = read_u32(offset) as usize;
31+
if offset < header::SIZE || offset > start_of_eoie {
32+
return None;
33+
}
34+
todo!("eoie")
35+
}
36+
}
37+
}
38+
39+
pub struct EndOfIndexEntry {
40+
/// The offset the the beginning of all extensions, or the end of all entries.
41+
offset_to_extensions: u32,
42+
/// The SHA1 checksum over the signature and size of all extensions.
43+
checksum: git_hash::ObjectId,
44+
}
45+
46+
impl EndOfIndexEntry {
47+
pub const SIGNATURE: &'static [u8] = b"EOIE";
48+
pub const SIZE: usize = 4 /* offset to extensions */ + git_hash::Kind::Sha1.len_in_bytes();
49+
pub const SIZE_WITH_HEADER: usize = crate::extension::MIN_SIZE + Self::SIZE;
50+
}

git-index/src/lib.rs

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,7 @@ use filetime::FileTime;
77

88
pub mod file;
99

10-
pub mod extension {
11-
use crate::{util::read_u32, Version};
12-
13-
const MIN_SIZE: usize = 4 /* signature */ + 4 /* size */;
14-
15-
fn decode_header(data: &[u8]) -> ([u8; 4], u32, &[u8]) {
16-
let (signature, data) = data.split_at(4);
17-
let (size, data) = data.split_at(4);
18-
(signature.try_into().unwrap(), read_u32(size), data)
19-
}
20-
21-
mod end_of_index_entry {
22-
use crate::{extension, extension::EndOfIndexEntry, file::header, util::read_u32};
23-
24-
impl EndOfIndexEntry {
25-
pub fn from_bytes(data: &[u8], object_hash: git_hash::Kind) -> Option<Self> {
26-
let hash_len = object_hash.len_in_bytes();
27-
if data.len() < EndOfIndexEntry::SIZE_WITH_HEADER + hash_len {
28-
return None;
29-
}
30-
31-
let start_of_eoie = data.len() - EndOfIndexEntry::SIZE_WITH_HEADER - hash_len;
32-
let data = &data[start_of_eoie..][..hash_len];
33-
34-
let (signature, ext_size, data) = extension::decode_header(data);
35-
if &signature != EndOfIndexEntry::SIGNATURE || ext_size as usize != EndOfIndexEntry::SIZE {
36-
return None;
37-
}
38-
39-
let (offset, hash) = data.split_at(4);
40-
let offset = read_u32(offset) as usize;
41-
if offset < header::SIZE {
42-
return None;
43-
}
44-
todo!("eoie")
45-
}
46-
}
47-
}
48-
49-
pub struct EndOfIndexEntry {
50-
/// The offset the the beginning of all extensions, or the end of all entries.
51-
offset_to_extensions: u32,
52-
/// The SHA1 checksum over the signature and size of all extensions.
53-
checksum: git_hash::ObjectId,
54-
}
55-
56-
impl EndOfIndexEntry {
57-
pub const SIGNATURE: &'static [u8] = b"EOIE";
58-
pub const SIZE: usize = 4 /* offset to extensions */ + git_hash::Kind::Sha1.len_in_bytes();
59-
pub const SIZE_WITH_HEADER: usize = crate::extension::MIN_SIZE + Self::SIZE;
60-
}
61-
}
10+
pub mod extension;
6211

6312
pub mod init {
6413
use filetime::FileTime;

0 commit comments

Comments
 (0)