Skip to content

Commit 9b28b18

Browse files
committed
refactor (#293)
1 parent 068c716 commit 9b28b18

File tree

4 files changed

+77
-73
lines changed

4 files changed

+77
-73
lines changed

git-index/src/file.rs

+27-68
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
pub mod init {
22
#![allow(unused)]
33

4-
use crate::file::decode;
5-
use crate::{File, State};
6-
use memmap2::Mmap;
74
use std::path::{Path, PathBuf};
85

6+
use memmap2::Mmap;
7+
8+
use crate::{extension, file::header, File, State};
9+
910
mod error {
10-
use crate::file::decode;
1111
use quick_error::quick_error;
1212

13+
use crate::file::header;
14+
1315
quick_error! {
1416
#[derive(Debug)]
1517
pub enum Error {
@@ -18,7 +20,7 @@ pub mod init {
1820
source(err)
1921
from()
2022
}
21-
DecodeHeader(err: decode::header::Error) {
23+
DecodeHeader(err: header::decode::Error) {
2224
display("The header could not be understood")
2325
source(err)
2426
from()
@@ -39,8 +41,8 @@ pub mod init {
3941
(data, filetime::FileTime::from_last_modification_time(&file.metadata()?))
4042
};
4143

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

4547
Ok(File {
4648
state: State { timestamp: mtime },
@@ -50,78 +52,40 @@ pub mod init {
5052
}
5153
}
5254

53-
pub mod decode {
54-
use crate::Version;
55-
56-
fn extension(data: &[u8]) -> ([u8; 4], u32, &[u8]) {
57-
let (signature, data) = data.split_at(4);
58-
let (size, data) = data.split_at(4);
59-
(signature.try_into().unwrap(), read_u32(size), data)
60-
}
61-
62-
pub(crate) mod extension {
63-
use crate::extension::EndOfIndexEntry;
64-
use crate::file::decode;
65-
use crate::file::decode::read_u32;
66-
67-
pub fn end_of_index_entry(data: &[u8], object_hash: git_hash::Kind) -> Option<EndOfIndexEntry> {
68-
let hash_len = object_hash.len_in_bytes();
69-
if data.len() < EndOfIndexEntry::SIZE_WITH_HEADER + hash_len {
70-
return None;
71-
}
72-
73-
let start_of_eoie = data.len() - EndOfIndexEntry::SIZE_WITH_HEADER - hash_len;
74-
let data = &data[start_of_eoie..][..hash_len];
55+
pub mod header {
56+
pub(crate) const SIZE: usize = 4 /*signature*/ + 4 /*version*/ + 4 /* num entries */;
7557

76-
let (signature, ext_size, data) = decode::extension(data);
77-
if &signature != EndOfIndexEntry::SIGNATURE || ext_size as usize != EndOfIndexEntry::SIZE {
78-
return None;
79-
}
80-
81-
let (offset, hash) = data.split_at(4);
82-
let offset = read_u32(offset) as usize;
83-
if offset < decode::header::SIZE {
84-
return None;
85-
}
86-
todo!("eoie")
87-
}
88-
}
58+
pub mod decode {
59+
use quick_error::quick_error;
8960

90-
pub mod header {
91-
pub(crate) const SIZE: usize = 4 /*signature*/ + 4 /*version*/ + 4 /* num entries */;
92-
93-
mod error {
94-
use quick_error::quick_error;
95-
96-
quick_error! {
97-
#[derive(Debug)]
98-
pub enum Error {
99-
Corrupt(message: &'static str) {
100-
display("{}", message)
101-
}
102-
UnsupportedVersion(version: u32) {
103-
display("Index version {} is not supported", version)
104-
}
61+
quick_error! {
62+
#[derive(Debug)]
63+
pub enum Error {
64+
Corrupt(message: &'static str) {
65+
display("{}", message)
66+
}
67+
UnsupportedVersion(version: u32) {
68+
display("Index version {} is not supported", version)
10569
}
10670
}
10771
}
108-
pub use error::Error;
10972
}
73+
use crate::{util::read_u32, Version};
11074

111-
pub(crate) fn header(
75+
pub(crate) fn decode(
11276
data: &[u8],
11377
object_hash: git_hash::Kind,
114-
) -> Result<(crate::Version, u32, &[u8]), header::Error> {
78+
) -> Result<(crate::Version, u32, &[u8]), decode::Error> {
11579
if data.len() < (3 * 4) + object_hash.len_in_bytes() {
116-
return Err(header::Error::Corrupt(
80+
return Err(decode::Error::Corrupt(
11781
"File is too small even for header with zero entries and smallest hash",
11882
));
11983
}
12084

12185
const SIGNATURE: &[u8] = b"DIRC";
12286
let (signature, data) = data.split_at(4);
12387
if signature != SIGNATURE {
124-
return Err(header::Error::Corrupt(
88+
return Err(decode::Error::Corrupt(
12589
"Signature mismatch - this doesn't claim to be a header file",
12690
));
12791
}
@@ -131,16 +95,11 @@ pub mod decode {
13195
2 => Version::V2,
13296
3 => Version::V3,
13397
4 => Version::V4,
134-
unknown => return Err(header::Error::UnsupportedVersion(unknown)),
98+
unknown => return Err(decode::Error::UnsupportedVersion(unknown)),
13599
};
136100
let (entries, data) = data.split_at(4);
137101
let entries = read_u32(entries);
138102

139103
Ok((version, entries, data))
140104
}
141-
142-
#[inline]
143-
fn read_u32(b: &[u8]) -> u32 {
144-
u32::from_be_bytes(b.try_into().unwrap())
145-
}
146105
}

git-index/src/lib.rs

+47-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
11
#![deny(unsafe_code, missing_docs, rust_2018_idioms)]
22
#![allow(missing_docs, unused)]
33

4-
use filetime::FileTime;
54
use std::path::PathBuf;
65

6+
use filetime::FileTime;
7+
78
pub mod file;
89

910
pub mod extension {
11+
use crate::{util::read_u32, Version};
12+
1013
const MIN_SIZE: usize = 4 /* signature */ + 4 /* size */;
1114

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+
1249
pub struct EndOfIndexEntry {
1350
/// The offset the the beginning of all extensions, or the end of all entries.
1451
offset_to_extensions: u32,
@@ -24,9 +61,10 @@ pub mod extension {
2461
}
2562

2663
pub mod init {
27-
use crate::State;
2864
use filetime::FileTime;
2965

66+
use crate::State;
67+
3068
impl State {
3169
/// Returns an empty state.
3270
/// TODO: figure out if it needs to know some configuration, and if this would actually be used somewhere
@@ -71,3 +109,10 @@ pub struct State {
71109
/// same timestamp as this as potentially changed, checking more thoroughly if a change actually happened.
72110
timestamp: FileTime,
73111
}
112+
113+
pub(crate) mod util {
114+
#[inline]
115+
pub fn read_u32(b: &[u8]) -> u32 {
116+
u32::from_be_bytes(b.try_into().unwrap())
117+
}
118+
}

git-odb/src/store_impls/dynamic/verify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::time::Instant;
21
use std::{
32
ops::Deref,
43
sync::atomic::{AtomicBool, Ordering},
4+
time::Instant,
55
};
66

77
use git_features::progress::{MessageLevel, Progress};

gitoxide-core/src/repository.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ pub fn init(directory: Option<PathBuf>) -> Result<git_repository::Path> {
88
}
99

1010
pub mod verify {
11-
use crate::pack;
12-
use crate::OutputFormat;
1311
use std::{path::PathBuf, sync::atomic::AtomicBool};
1412

1513
use git_repository::Progress;
1614

15+
use crate::{pack, OutputFormat};
16+
1717
/// A general purpose context for many operations provided here
1818
pub struct Context {
1919
/// If set, provide statistics to `out` in the given format

0 commit comments

Comments
 (0)