Skip to content

Commit 389e828

Browse files
committed
Add checksum to pack (in preparation for validation)
1 parent 1efa46f commit 389e828

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ trailed by a newline.
164164
echo c56a8e7aa92c86c41a923bc760d2dc39e8a31cf7 | git cat-file --batch | tail +2 > fixture
165165
```
166166

167-
Thus one has to post-process the file by reducing its size by one using `truncate -s -1 fixture`, removing the newline byte.
167+
Thus one has to post-process the file by reducing its size by one using `truncate -s -1 fixture`, **removing the newline byte**.
168168

169169

170170
## Shortcomings

gitoxide-odb/src/pack/file/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use byteorder::{BigEndian, ByteOrder};
22
use filebuffer::FileBuffer;
3-
use git_object::SHA1_SIZE;
3+
use git_object::{Id, SHA1_SIZE};
44
use quick_error::quick_error;
55
use std::{convert::TryFrom, convert::TryInto, mem::size_of, path::Path};
66

@@ -58,6 +58,11 @@ impl File {
5858
pub fn num_objects(&self) -> u32 {
5959
self.num_objects
6060
}
61+
pub fn checksum(&self) -> Id {
62+
let mut v = [0u8; 20];
63+
v.copy_from_slice(&self.data[self.data.len() - SHA1_SIZE..]);
64+
v
65+
}
6166

6267
fn assure_v2(&self) {
6368
assert!(

gitoxide-odb/tests/pack/file.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@ use crate::fixture_path;
22
use git_odb::pack;
33
use std::convert::TryFrom;
44

5-
fn new_pack(at: &str) -> pack::File {
5+
fn pack_at(at: &str) -> pack::File {
66
pack::File::try_from(fixture_path(at).as_path()).unwrap()
77
}
88

9+
mod method {
10+
use crate::pack::file::pack_at;
11+
use crate::pack::SMALL_PACK;
12+
13+
#[test]
14+
fn checksum() {
15+
let p = pack_at(SMALL_PACK);
16+
assert_eq!(
17+
hex::encode(p.checksum()),
18+
"0f3ea84cd1bba10c2a03d736a460635082833e59"
19+
);
20+
}
21+
}
22+
923
/// All hardcoded offsets are obtained via `git verify-pack --verbose tests/fixtures/packs/pack-a2bf8e71d8c18879e499335762dd95119d93d9f1.idx`
1024
mod decode_entry {
11-
use crate::{pack::file::new_pack, pack::SMALL_PACK};
25+
use crate::{pack::file::pack_at, pack::SMALL_PACK};
1226
use bstr::ByteSlice;
1327
use git_odb::pack::ResolvedBase;
1428

@@ -44,7 +58,7 @@ mod decode_entry {
4458
panic!("should not want to resolve an id here")
4559
}
4660

47-
let p = new_pack(SMALL_PACK);
61+
let p = pack_at(SMALL_PACK);
4862
let entry = p.entry(offset);
4963
let mut buf = Vec::new();
5064
p.decode_entry(entry, &mut buf, resolve_with_panic).unwrap();
@@ -53,7 +67,7 @@ mod decode_entry {
5367
}
5468

5569
mod decompress_entry {
56-
use crate::{pack::file::new_pack, pack::SMALL_PACK};
70+
use crate::{pack::file::pack_at, pack::SMALL_PACK};
5771
use bstr::ByteSlice;
5872

5973
#[test]
@@ -93,7 +107,7 @@ mod decompress_entry {
93107
}
94108

95109
fn decompress_entry_at_offset(offset: u64) -> Vec<u8> {
96-
let p = new_pack(SMALL_PACK);
110+
let p = pack_at(SMALL_PACK);
97111
let entry = p.entry(offset);
98112

99113
let size = entry.size as usize;

0 commit comments

Comments
 (0)