Skip to content

Commit

Permalink
First basic index file verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jun 25, 2020
1 parent db09a6b commit 994700f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ The CLI uses various crates, please see _'Development Status'_ for details.

## Development Status

* **gitoxide**
* please note that all functionality comes from the `gitoxide-core` library, which mirrors these capabilities
and itself relies on all `git-*` crates.
* repository
* [x] init
* plumbing
* [x] pack verify
* [x] pack index verify including each object sha1
* **git-repository**
* [x] initialize
* [ ] read and write all data types
Expand Down
25 changes: 22 additions & 3 deletions gitoxide-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use std::{io, path::Path};

pub fn init() -> Result<()> {
git_repository::init::repository().with_context(|| "Repository initialization failed")
}

pub fn verify_pack_or_pack_index(path: impl AsRef<Path>, mut out: impl io::Write) -> Result<()> {
let pack = git_odb::pack::File::at(path).with_context(|| "Could not open pack file")?;
pack.verify_checksum().with_context(|| "Failed")?;
let path = path.as_ref();
let ext = path.extension()
.and_then(|ext| ext.to_str())
.ok_or_else(|| anyhow!("Cannot determine file type on path without extension '{}', expecting default extensions 'idx' and 'pack'", path.display()))?;
match ext {
"pack" => {
let pack = git_odb::pack::File::at(path).with_context(|| "Could not open pack file")?;
pack.verify_checksum()?;
}
"idx" => {
let idx = git_odb::pack::index::File::at(path)
.with_context(|| "Could not open pack index file")?;
idx.verify_checksum_of_index()?;
}
ext => {
return Err(anyhow!(
"Unknown extension {:?}, expecting 'idx' or 'pack'",
ext
))
}
}
writeln!(out, "OK")?;
Ok(())
}
1 change: 1 addition & 0 deletions tests/snapshots/plumbing-verify-pack-index-success
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OK
7 changes: 7 additions & 0 deletions tests/stateless-journey.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ title "CLI"
expect_run $SUCCESSFULLY "$exe" plumbing verify-pack "$PACK_FILE"
}
)
(with "a valid pack file"
PACK_INDEX_FILE="$fixtures/packs/pack-c0438c19fb16422b6bbcce24387b3264416d485b.idx"
it "verifies the pack index successfully and with desired output" && {
WITH_SNAPSHOT="$snapshot/plumbing-verify-pack-index-success" \
expect_run $SUCCESSFULLY "$exe" plumbing verify-pack "$PACK_INDEX_FILE"
}
)
)
)

0 comments on commit 994700f

Please sign in to comment.