Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[commitgraph] Implement commit-graph-verify plumbing command. #26

Merged
merged 9 commits into from
Oct 13, 2020
5 changes: 4 additions & 1 deletion Cargo.lock

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

7 changes: 5 additions & 2 deletions git-commitgraph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ include = ["src/**/*"]
[lib]
doctest = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
serde1 = ["serde", "git-object/serde1"]

[dependencies]
git-features = { version = "^0.6.0", path = "../git-features" }
git-object = { version = "^0.4.0", path = "../git-object" }

bstr = { version = "0.2.13", default-features = false, features = ["std"] }
byteorder = "1.2.3"
filebuffer = "0.4.0"
quick-error = "2.0.0"
serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"] }
thiserror = "1.0.20"

[dev-dependencies]
tempfile = "3.1.0"
14 changes: 8 additions & 6 deletions git-commitgraph/src/file/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use std::{

/// Access
impl File {
pub fn base_graph_count(&self) -> u8 {
self.base_graph_count
}

/// Returns the commit data for the commit located at the given lexigraphical position.
///
/// `pos` must range from 0 to self.num_commits().
Expand Down Expand Up @@ -42,12 +46,10 @@ impl File {
}

pub fn iter_base_graph_ids(&self) -> impl Iterator<Item = borrowed::Id<'_>> {
let base_graphs_list = match self.base_graphs_list_offset {
Some(v) => &self.data[v..v + (SHA1_SIZE * self.base_graph_count as usize)],
None => &[],
};
let start = self.base_graphs_list_offset.unwrap_or(0);
let base_graphs_list = &self.data[start..start + (SHA1_SIZE * usize::from(self.base_graph_count))];
base_graphs_list
.chunks_exact(SHA1_SIZE)
.chunks(SHA1_SIZE)
.map(|bytes| borrowed::Id::try_from(bytes).expect("20 bytes SHA1 to be alright"))
}

Expand All @@ -61,7 +63,7 @@ impl File {

// copied from git-odb/src/pack/index/access.rs
pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<file::Position> {
let first_byte = id.first_byte() as usize;
let first_byte = usize::from(id.first_byte());
let mut upper_bound = self.fan[first_byte];
let mut lower_bound = if first_byte != 0 { self.fan[first_byte - 1] } else { 0 };

Expand Down
54 changes: 26 additions & 28 deletions git-commitgraph/src/file/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,22 @@ use crate::{
};
use byteorder::{BigEndian, ByteOrder};
use git_object::{borrowed, owned, SHA1_SIZE};
use quick_error::quick_error;
use std::{
convert::{TryFrom, TryInto},
fmt::{Debug, Formatter},
slice::Chunks,
};

quick_error! {
#[derive(Debug)]
pub enum Error {
ExtraEdgesListOverflow(commit: owned::Id) {
display(
"commit {}'s extra edges overflows the commit-graph file's extra edges list",
commit,
)
}
FirstParentIsExtraEdgeIndex(commit: owned::Id) {
display(
"commit {}'s first parent is an extra edge index, which is invalid",
commit,
)
}
MissingExtraEdgesList(commit: owned::Id) {
display(
"commit {} has extra edges, but commit-graph file has no extra edges list",
commit,
)
}
SecondParentWithoutFirstParent(commit: owned::Id) {
display("commit {} has a second parent but not a first parent", commit)
}
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("commit {0}'s extra edges overflows the commit-graph file's extra edges list")]
ExtraEdgesListOverflow(owned::Id),
#[error("commit {0}'s first parent is an extra edge index, which is invalid")]
FirstParentIsExtraEdgeIndex(owned::Id),
#[error("commit {0} has extra edges, but commit-graph file has no extra edges list")]
MissingExtraEdgesList(owned::Id),
#[error("commit {0} has a second parent but not a first parent")]
SecondParentWithoutFirstParent(owned::Id),
}

// Note that git's commit-graph-format.txt as of v2.28.0 gives an incorrect value 0x0700_0000 for
Expand Down Expand Up @@ -93,15 +77,29 @@ impl<'a> Commit<'a> {
}
}

pub fn id(&self) -> borrowed::Id<'_> {
// Allow the return value to outlive this Commit object, as it only needs to be bound by the
// lifetime of the parent file.
pub fn id<'b>(&'b self) -> borrowed::Id<'a>
where
'a: 'b,
{
self.file.id_at(self.pos)
}

pub fn parent1(&self) -> Result<Option<graph::Position>, Error> {
self.iter_parents().next().transpose()
}

pub fn root_tree_id(&self) -> borrowed::Id<'_> {
pub fn position(&self) -> file::Position {
self.pos
}

// Allow the return value to outlive this Commit object, as it only needs to be bound by the
// lifetime of the parent file.
pub fn root_tree_id<'b>(&'b self) -> borrowed::Id<'a>
where
'a: 'b,
{
self.root_tree_id
}
}
Expand Down
Loading