Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d5eef9c

Browse files
committed
document loose::Object entirely
1 parent b9e0a87 commit d5eef9c

6 files changed

Lines changed: 24 additions & 1 deletion

File tree

git-odb/src/loose/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! An object database storing each object in a zlib compressed file with its hash in the path
12
const HEADER_READ_COMPRESSED_BYTES: usize = 256;
23
const HEADER_READ_UNCOMPRESSED_BYTES: usize = 512;
34

git-odb/src/loose/object/decode.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,25 @@ pub enum Error {
2020
},
2121
}
2222

23+
// Decoding and streaming
2324
impl loose::Object {
24-
/// **Note**: Blobs are loaded into memory and are made available that way.
25+
/// Decode the object to make it's fields accessible in case of Trees, Tags and Commits.
26+
///
27+
/// This is a zero-copy operation with data read from disk if needed and stored in memory.
28+
/// The returned [`borrowed::Object`] references this data where possible.
29+
///
30+
/// **Note**: Blobs are also loaded into memory and are made available that way.
2531
/// Consider using `stream()` if large Blobs are expected.
2632
pub fn decode(&mut self) -> Result<borrowed::Object<'_>, Error> {
2733
self.decompress_all()?;
2834
let bytes = &self.decompressed_data[self.header_size..];
2935
Ok(borrowed::Object::from_bytes(self.kind, bytes)?)
3036
}
3137

38+
/// Returns an implementation of [`std::io::Read`], which decompresses the objects data on the fly.
39+
///
40+
/// **Note**: This is most useful for big blobs as these won't be read into memory in full. Use [`decode()`][loose::Object::decode()] for
41+
/// Trees, Tags and Commits instead for convenient access to their payload.
3242
pub fn stream(&mut self) -> Result<stream::Reader<'_>, Error> {
3343
match &self.path {
3444
Some(path) => Ok(stream::Reader::from_read(

git-odb/src/loose/object/header.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! loose object header encoding and decoding
12
use byteorder::WriteBytesExt;
23
use git_object as object;
34

@@ -15,6 +16,9 @@ pub enum Error {
1516
ObjectHeader(#[from] object::Error),
1617
}
1718

19+
/// Decode a loose object header, being `<kind> <size>\0`, returns ([`Kind`][object::Kind], `size`, `consumed bytes`).
20+
///
21+
/// `size` is the uncompressed size of the payload in bytes.
1822
pub fn decode(input: &[u8]) -> Result<(object::Kind, u64, usize), Error> {
1923
let header_end = input
2024
.iter()
@@ -46,6 +50,7 @@ fn kind_to_bytes_with_space(object: object::Kind) -> &'static [u8] {
4650
}
4751
}
4852

53+
/// Encode the objects `Kind` and `size` into a format suitable for use with [`decode()`].
4954
pub fn encode(object: object::Kind, size: u64, mut out: impl std::io::Write) -> Result<usize, std::io::Error> {
5055
let mut written = out.write(kind_to_bytes_with_space(object))?;
5156
written += itoa::write(&mut out, size)?;

git-odb/src/loose/object/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ use git_object as object;
33
use smallvec::SmallVec;
44
use std::path::PathBuf;
55

6+
/// A representation of a loose object on disk, which is fully or partially read into memory
67
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
78
pub struct Object {
9+
/// The kind of object
810
pub kind: object::Kind,
11+
/// The uncompressed size of the object's data/payload
912
pub size: usize,
1013
pub(crate) decompressed_data: SmallVec<[u8; HEADER_READ_UNCOMPRESSED_BYTES]>,
1114
pub(crate) compressed_data: SmallVec<[u8; HEADER_READ_COMPRESSED_BYTES]>,

git-odb/src/loose/object/stream.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub enum Reader<'a> {
66
Buffer(&'a [u8]),
77
}
88

9+
/// A [`Read`][std::io::Read] implementation for reading from a file or from borrowed data.
910
impl<'a> Reader<'a> {
1011
pub fn from_read(header_size: usize, file: std::fs::File) -> Reader<'a> {
1112
Reader::File(header_size, InflateReader::from_read(std::io::BufReader::new(file)))

git-odb/src/loose/object/verify.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub enum Error {
1313
}
1414

1515
impl loose::Object {
16+
/// Generate the git hash of this object, reading it in the process, and compare it with the given `desired` [Id][borrowed::Id].
17+
///
18+
/// Returns an error with the actual id if the hashes don't match.
1619
pub fn verify_checksum(&mut self, desired: borrowed::Id<'_>) -> Result<(), Error> {
1720
let mut sink = HashWrite::new(io::sink(), desired.kind());
1821
let (kind, size) = (self.kind, self.size);

0 commit comments

Comments
 (0)