1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#![allow(unused)]
use std::path::{Path, PathBuf};
use memmap2::Mmap;
use crate::{decode, extension, File, State};
mod error {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("An IO error occurred while opening the index")]
Io(#[from] std::io::Error),
#[error(transparent)]
Decode(#[from] crate::decode::Error),
}
}
pub use error::Error;
impl File {
pub fn at(path: impl Into<PathBuf>, object_hash: git_hash::Kind, options: decode::Options) -> Result<Self, Error> {
let path = path.into();
let (data, mtime) = {
let file = std::fs::File::open(&path)?;
#[allow(unsafe_code)]
let data = unsafe { Mmap::map(&file)? };
(data, filetime::FileTime::from_last_modification_time(&file.metadata()?))
};
let (state, checksum) = State::from_bytes(&data, mtime, object_hash, options)?;
Ok(File {
state,
path,
checksum: Some(checksum),
})
}
pub fn from_state(state: crate::State, path: impl Into<PathBuf>) -> Self {
File {
state,
path: path.into(),
checksum: None,
}
}
}