Skip to content

Commit

Permalink
change!: introduce `index::File::verify_integrity(…, pack: Option<Pac…
Browse files Browse the repository at this point in the history
…kContext>, …)`, replacing tuple (#279)

This allows for more documentation on what input is required there and
generally makes for an easier to use API.
  • Loading branch information
Byron committed Dec 29, 2021
1 parent 853d468 commit 80b120d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 17 deletions.
7 changes: 6 additions & 1 deletion git-pack/src/bundle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ mod verify {
C: crate::cache::DecodeEntry,
{
self.index.verify_integrity(
Some((&self.pack, verify_mode, traversal, make_pack_lookup_cache)),
Some(crate::index::verify::PackContext {
data: &self.pack,
verify_mode: verify_mode,
traversal_algorithm: traversal,
make_cache_fn: make_pack_lookup_cache,
}),
thread_limit,
progress,
should_interrupt,
Expand Down
39 changes: 28 additions & 11 deletions git-pack/src/index/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub mod integrity {
expected: BString,
actual: BString,
},
#[error(transparent)]
ObjectEncode(#[from] std::io::Error),
}
}

Expand All @@ -50,6 +48,22 @@ pub enum Mode {
Sha1Crc32DecodeEncode,
}

/// Information to allow verifying the integrity of an index with the help of its corresponding pack.
pub struct PackContext<'a, C, F>
where
C: crate::cache::DecodeEntry,
F: Fn() -> C + Send + Clone,
{
/// The pack data file itself.
pub data: &'a crate::data::File,
/// the way to verify the pack data.
pub verify_mode: Mode,
/// The traversal algorithm to use
pub traversal_algorithm: index::traverse::Algorithm,
/// A function to create a pack cache for each tread.
pub make_cache_fn: F,
}

/// Verify and validate the content of the index file
impl index::File {
/// Returns the trailing hash stored at the end of this index file.
Expand Down Expand Up @@ -101,14 +115,9 @@ impl index::File {
///
/// The given `progress` is inevitably consumed if there is an error, which is a tradeoff chosen to easily allow using `?` in the
/// error case.
pub fn verify_integrity<C, P>(
pub fn verify_integrity<P, C, F>(
&self,
pack: Option<(
&crate::data::File,
Mode,
index::traverse::Algorithm,
impl Fn() -> C + Send + Clone,
)>,
pack: Option<PackContext<'_, C, F>>,
thread_limit: Option<usize>,
progress: Option<P>,
should_interrupt: Arc<AtomicBool>,
Expand All @@ -119,10 +128,16 @@ impl index::File {
where
P: Progress,
C: crate::cache::DecodeEntry,
F: Fn() -> C + Send + Clone,
{
let mut root = progress::DoOrDiscard::from(progress);
match pack {
Some((pack, mode, algorithm, make_cache)) => self
Some(PackContext {
data: pack,
verify_mode: mode,
traversal_algorithm: algorithm,
make_cache_fn: make_cache,
}) => self
.traverse(
pack,
root.into_inner(),
Expand Down Expand Up @@ -173,7 +188,9 @@ impl index::File {
})?;
if let Mode::Sha1Crc32DecodeEncode = mode {
encode_buf.clear();
object.write_to(&mut *encode_buf)?;
object
.write_to(&mut *encode_buf)
.expect("writing to a memory buffer never fails");
if encode_buf.as_slice() != buf {
let mut should_return_error = true;
if let git_object::Kind::Tree = object_kind {
Expand Down
3 changes: 2 additions & 1 deletion git-pack/src/multi_index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ pub struct File {
///
mod access;

mod verify;
///
pub mod verify;

///
pub mod chunk;
Expand Down
1 change: 1 addition & 0 deletions git-pack/src/multi_index/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::multi_index::File;
use git_features::progress::Progress;
use std::sync::atomic::AtomicBool;

///
pub mod checksum {
/// Returned by [`index::File::verify_checksum()`][crate::index::File::verify_checksum()].
pub type Error = crate::verify::checksum::Error;
Expand Down
9 changes: 7 additions & 2 deletions git-pack/tests/pack/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,12 @@ mod file {
for mode in MODES {
assert_eq!(
idx.verify_integrity(
Some((&pack, *mode, *algo, || cache::Never)),
Some(git_pack::index::verify::PackContext {
data: &pack,
verify_mode: *mode,
traversal_algorithm: *algo,
make_cache_fn: || cache::Never
}),
None,
progress::Discard.into(),
Default::default()
Expand Down Expand Up @@ -399,7 +404,7 @@ mod file {
assert_eq!(idx.num_objects(), *num_objects);
assert_eq!(
idx.verify_integrity(
None::<(_, _, _, fn() -> cache::Never)>,
None::<git_pack::index::verify::PackContext<'_, _, fn() -> cache::Never>>,
None,
progress::Discard.into(),
Default::default()
Expand Down
10 changes: 8 additions & 2 deletions gitoxide-core/src/pack/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{

use anyhow::{anyhow, Context as AnyhowContext, Result};
use bytesize::ByteSize;
use git_repository as git;
use git_repository::{
easy::object,
hash::ObjectId,
Expand Down Expand Up @@ -128,7 +129,7 @@ where
path.display()
)
})?;
let object_hash = git_repository::hash::Kind::Sha1; // TODO: make it configurable via Context/CLI
let object_hash = git::hash::Kind::Sha1; // TODO: make it configurable via Context/CLI
let res = match ext {
"pack" => {
let pack = odb::pack::data::File::at(path, object_hash).with_context(|| "Could not open pack file")?;
Expand Down Expand Up @@ -169,7 +170,12 @@ where
};

idx.verify_integrity(
pack.as_ref().map(|p| (p, mode, algorithm.into(), cache)),
pack.as_ref().map(|p| git::odb::pack::index::verify::PackContext {
data: p,
verify_mode: mode,
traversal_algorithm: algorithm.into(),
make_cache_fn: cache,
}),
thread_limit,
progress,
should_interrupt,
Expand Down

0 comments on commit 80b120d

Please sign in to comment.