History

[git-pack] move hash-writer to git-features as it's quite general purpose

Sebastian Thiel committed 5 years ago

Commit 80e56401 parent 93a50a08 files · +55 −48Browse files

Unified · Split

8 files
  1. git-features/src/hash.rs +38 −0
  2. git-pack/src/data.rs +2 −1
  3. git-pack/src/hash.rs +0 −36
  4. git-pack/src/lib.rs +1 −4
  5. git-pack/src/pack/data/input/iter.rs +6 −3
  6. git-pack/src/pack/data/output/entries_to_bytes.rs +2 −1
  7. git-pack/src/pack/index/write/encode.rs +5 −2
  8. git-pack/src/pack/index/write/mod.rs +1 −1
git-features/src/hash.rs+38−0
@@ -118,3 +118,41 @@ pub fn bytes_of_file( · 117 unmodified lines
118118 progress.show_throughput(start);
119119 Ok(id)
120120 }
121+
122+/// A utility to automatically generate a hash while writing into an inner writer.
123+pub struct Write<T> {
124+ /// The hash implementation.
125+ pub hash: Sha1,
126+ /// The inner writer.
127+ pub inner: T,
128+}
129+
130+impl<T> std::io::Write for Write<T>
131+where
132+ T: std::io::Write,
133+{
134+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
135+ let written = self.inner.write(buf)?;
136+ self.hash.update(&buf[..written]);
137+ Ok(written)
138+ }
139+
140+ fn flush(&mut self) -> std::io::Result<()> {
141+ self.inner.flush()
142+ }
143+}
144+
145+impl<T> Write<T>
146+where
147+ T: std::io::Write,
148+{
149+ /// Create a new hash writer which hashes all bytes written to `inner` with a hash of `kind`.
150+ pub fn new(inner: T, kind: git_hash::Kind) -> Self {
151+ match kind {
152+ git_hash::Kind::Sha1 => Write {
153+ inner,
154+ hash: Sha1::default(),
155+ },
156+ }
157+ }
158+}
git-pack/src/data.rs+2−1
@@ -67,7 +67,8 @@ impl<'a> Object<'a> { · 66 unmodified lines
6767
6868 /// Types supporting object hash verification
6969 pub mod verify {
70− use crate::{hash, loose};
70+ use crate::loose;
71+ use git_features::hash;
7172 use std::io;
7273
7374 /// Returned by [`crate::data::Object::verify_checksum()`]
git-pack/src/hash.rs+0−36
@@ -1,36 +0,0 @@
1−use git_features::hash;
2−use std::io;
3−
4−pub(crate) struct Write<T> {
5− pub hash: hash::Sha1,
6− pub inner: T,
7−}
8−
9−impl<T> io::Write for Write<T>
10−where
11− T: io::Write,
12−{
13− fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
14− let written = self.inner.write(buf)?;
15− self.hash.update(&buf[..written]);
16− Ok(written)
17− }
18−
19− fn flush(&mut self) -> io::Result<()> {
20− self.inner.flush()
21− }
22−}
23−
24−impl<T> Write<T>
25−where
26− T: io::Write,
27−{
28− pub fn new(inner: T, kind: git_hash::Kind) -> Self {
29− match kind {
30− git_hash::Kind::Sha1 => Write {
31− inner,
32− hash: hash::Sha1::default(),
33− },
34− }
35− }
36−}
git-pack/src/lib.rs+1−4
@@ -19,11 +19,8 @@ · 18 unmodified lines
1919 //! * [`store::linked::Db`]
2020 //! * A database containing various [`compound::Backends`][store::compound::Backend] as gathered from `alternates` files.
2121
22−pub mod pack;
23−
24−pub(crate) mod hash;
25−
2622 pub mod data;
23+pub mod pack;
2724
2825 ///
2926 pub mod find;
git-pack/src/pack/data/input/iter.rs+6−3
@@ -1,6 +1,9 @@
1−use crate::{hash, pack, pack::data::input};
2−use git_features::hash::Sha1;
3−use git_features::zlib::{stream::inflate::ReadBoxed, Decompress};
1+use crate::{pack, pack::data::input};
2+use git_features::{
3+ hash,
4+ hash::Sha1,
5+ zlib::{stream::inflate::ReadBoxed, Decompress},
6+};
47 use std::{fs, io};
58
69 /// An iterator over [`Entries`][input::Entry] in a byte stream.
git-pack/src/pack/data/output/entries_to_bytes.rs+2−1
@@ -1,4 +1,5 @@
1−use crate::{hash, pack, pack::data::output};
1+use crate::{pack, pack::data::output};
2+use git_features::hash;
23 use std::io::Write;
34
45 /// The error returned by `next()` in the [`EntriesToBytesIter`] iterator.
git-pack/src/pack/index/write/encode.rs+5−2
@@ -1,9 +1,12 @@
11 use crate::{
2− hash, pack,
2+ pack,
33 pack::index::{util::Count, V2_SIGNATURE},
44 };
55 use byteorder::{BigEndian, WriteBytesExt};
6−use git_features::progress::{self, Progress};
6+use git_features::{
7+ hash,
8+ progress::{self, Progress},
9+};
710 use std::{cmp::Ordering, collections::VecDeque, io};
811
912 pub(crate) fn write_to(
git-pack/src/pack/index/write/mod.rs+1−1
@@ -221,7 +221,7 @@ fn modify_base( · 220 unmodified lines
221221 hash: git_hash::Kind,
222222 ) {
223223 fn compute_hash(kind: git_object::Kind, bytes: &[u8], hash_kind: git_hash::Kind) -> git_hash::ObjectId {
224− let mut write = crate::hash::Write::new(io::sink(), hash_kind);
224+ let mut write = git_features::hash::Write::new(io::sink(), hash_kind);
225225 loose::object::header::encode(kind, bytes.len() as u64, &mut write)
226226 .expect("write to sink and hash cannot fail");
227227 write.hash.update(bytes);