#![allow(unused)] use std::path::{Path, PathBuf}; use memmap2::Mmap; use crate::{decode, extension, File, State}; mod error { /// The error returned by [File::at()][super::File::at()]. #[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; /// Initialization impl File { /// Open an index file at `path` with `options`, assuming `object_hash` is used throughout the file. pub fn at(path: impl Into, object_hash: git_hash::Kind, options: decode::Options) -> Result { let path = path.into(); let (data, mtime) = { // SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file. 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), }) } /// Consume `state` and pretend it was read from `path`, setting our checksum to `null`. /// /// `File` instances created like that should be written to disk to set the correct checksum via `[File::write()]`. pub fn from_state(state: crate::State, path: impl Into) -> Self { File { state, path: path.into(), checksum: None, } } }