Skip to content

Commit

Permalink
first stab at basic index file parsing (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jan 9, 2022
1 parent 494ed46 commit 826ca0c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ check: ## Build all code in suitable configurations
&& cargo check
cd git-object && cargo check --all-features \
&& cargo check --features verbose-object-parsing-errors
cd git-index && cargo check --features serde1
cd git-actor && cargo check --features serde1
cd git-pack && cargo check --features serde1 \
&& cargo check --features pack-cache-lru-static \
Expand Down
6 changes: 6 additions & 0 deletions git-index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ edition = "2018"
[lib]
doctest = false

[features]
serde1 = ["serde"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
git-hash = { version ="^0.8.0", path = "../git-hash" }

quick-error = "2.0.0"
memmap2 = "0.5.0"
filetime = "0.2.15"

serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"] }

[dev-dependencies]
git-testtools = { path = "../tests/tools"}
48 changes: 40 additions & 8 deletions git-index/src/file.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod init {
#![allow(unused)]
use crate::File;
use crate::{File, State};
use memmap2::Mmap;
use std::path::Path;
use std::path::{Path, PathBuf};

mod error {
use quick_error::quick_error;
Expand All @@ -11,7 +11,7 @@ pub mod init {
#[derive(Debug)]
pub enum Error {
Io(err: std::io::Error) {
display("An IO error occurred while reading the index")
display("An IO error occurred while opening the index")
source(err)
from()
}
Expand All @@ -21,12 +21,44 @@ pub mod init {
pub use error::Error;

impl File {
pub fn at(path: impl AsRef<Path>, object_hash: git_hash::Kind) -> Result<Self, Error> {
// SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file.
#[allow(unsafe_code)]
let data = unsafe { Mmap::map(&std::fs::File::open(path)?)? };
pub fn at(path: impl Into<PathBuf>, object_hash: git_hash::Kind) -> Result<Self, Error> {
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()?))
};

todo!("read file")
Ok(File {
state: State { timestamp: mtime },
path,
})
}
}
}

pub mod decode {
pub mod header {
mod error {
use quick_error::quick_error;

quick_error! {
#[derive(Debug)]
pub enum Error {
Io(err: std::io::Error) {
display("An IO error occurred while opening the index")
source(err)
from()
}
}
}
}
pub use error::Error;
}

fn header(data: &[u8]) -> Result<(crate::Version, &[u8]), header::Error> {
todo!("header parsing")
}
}
30 changes: 25 additions & 5 deletions git-index/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#![deny(unsafe_code, missing_docs, rust_2018_idioms)]
#![allow(missing_docs)]
#![allow(missing_docs, unused)]

use filetime::FileTime;
use std::path::PathBuf;

pub mod file;

pub mod init {
use crate::State;
use filetime::FileTime;

impl State {
/// Returns an empty state.
/// TODO: figure out if it needs to know some configuration
pub fn new() -> Self {
State
/// TODO: figure out if it needs to know some configuration, and if this would actually be used somewhere
fn new() -> Self {
State {
timestamp: FileTime::from_system_time(std::time::SystemTime::UNIX_EPOCH),
}
}
}

Expand All @@ -23,6 +27,16 @@ pub mod init {
}
}

/// All known versions of a git index file.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum Version {
V2 = 2,
V3 = 3,
V4 = 4,
}

/// An index file whose state was read from a file on disk.
pub struct File {
pub state: State,
Expand All @@ -33,4 +47,10 @@ pub struct File {
///
/// As opposed to a snapshot, it's meant to be altered and eventually be written back to disk or converted into a tree.
/// We treat index and its state synonymous.
pub struct State;
pub struct State {
/// The time at which the state was created, indicating its freshness compared to other files on disk.
///
/// Note that on platforms that only have a precisions of a second for this time, we will treat all entries with the
/// same timestamp as this as potentially changed, checking more thoroughly if a change actually happened.
timestamp: FileTime,
}
1 change: 0 additions & 1 deletion git-index/tests/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod init {
}

#[test]
#[ignore]
fn read_v2() {
let _file = file("v2");
}
Expand Down

0 comments on commit 826ca0c

Please sign in to comment.