pub struct Pack {
pub hash_kind: HashKind,
pub number: usize,
pub signature: ObjectHash,
pub objects: Vec<Box<dyn ObjectTrait>>,
pub pool: Arc<ThreadPool>,
pub waitlist: Arc<Waitlist>,
pub caches: Arc<Caches>,
pub mem_limit: Option<usize>,
pub cache_objs_mem: Arc<AtomicUsize>,
pub clean_tmp: bool,
}Expand description
Representation of a Git pack file in memory.
Fields§
§hash_kind: HashKindRepository hash kind used for every object ID, ref-delta base, pack trailer and idx checksum this pack touches (explicit; never read from a worker’s thread-local).
number: usize§signature: ObjectHash§objects: Vec<Box<dyn ObjectTrait>>§pool: Arc<ThreadPool>§waitlist: Arc<Waitlist>§caches: Arc<Caches>§mem_limit: Option<usize>§cache_objs_mem: Arc<AtomicUsize>§clean_tmp: boolImplementations§
Source§impl Pack
impl Pack
Sourcepub fn new(
thread_num: Option<usize>,
mem_limit: Option<usize>,
temp_path: Option<PathBuf>,
clean_tmp: bool,
) -> Self
pub fn new( thread_num: Option<usize>, mem_limit: Option<usize>, temp_path: Option<PathBuf>, clean_tmp: bool, ) -> Self
§Parameters
thread_num: The number of threads to use for decoding and cache,Nonemeans use the number of logical CPUs. The requested value is capped by available parallelism to avoid over-threading in constrained containers. It can’t be zero, or panicmem_limit: The maximum size of the memory cache in bytes, or None for unlimited. The 80% of it will be used for Caches. Very large limits use the direct in-memory cache path and skip per-object memory backpressure to avoid hot-loop cache accounting.
Not very accurate, because of memory alignment and other reasons, overuse about 15%temp_path: The path to a directory for temporary files, default is “./.cache_temp”
For example, thread_num = 4 will use up to 8 threads (4 for decoding and 4 for cache)clean_tmp: whether to remove temp directory when Pack is dropped
Sourcepub fn new_with_hash_kind(
hash_kind: HashKind,
thread_num: Option<usize>,
mem_limit: Option<usize>,
temp_path: Option<PathBuf>,
clean_tmp: bool,
) -> Self
pub fn new_with_hash_kind( hash_kind: HashKind, thread_num: Option<usize>, mem_limit: Option<usize>, temp_path: Option<PathBuf>, clean_tmp: bool, ) -> Self
Like Pack::new, but for an explicit repository hash_kind.
Every object ID, ref-delta base, pack trailer and idx checksum handled by this
pack is computed and parsed with hash_kind; worker threads spawned by the
decoder inherit it explicitly instead of reading their own thread-local.
Pack::new is the thread-local compatibility wrapper.
Sourcepub fn check_header(pack: &mut impl BufRead) -> Result<(u32, Vec<u8>), GitError>
pub fn check_header(pack: &mut impl BufRead) -> Result<(u32, Vec<u8>), GitError>
Checks and reads the header of a Git pack file.
This function reads the first 12 bytes of a pack file, which include the b“PACK“ magic identifier, the version number, and the number of objects in the pack. It verifies that the magic identifier is correct and that the version number is 2 (which is the version currently supported by Git). It also collects these header bytes for later use, such as for hashing the entire pack file.
§Parameters
pack- A mutable reference to an object implementing theReadtrait, representing the source of the pack file data (e.g., file, memory stream).
§Returns
A Result which is:
Ok((u32, Vec<u8>)): On successful reading and validation of the header, returns a tuple where:- The first element is the number of objects in the pack file (
u32). - The second element is a vector containing the bytes of the pack file header (
Vec<u8>).
- The first element is the number of objects in the pack file (
Err(GitError): On failure, returns aGitErrorwith a description of the issue.
§Errors
This function can return an error in the following situations:
- If the pack file does not start with the “PACK” magic identifier.
- If the pack file’s version number is not 2.
- If there are any issues reading from the provided
packsource.
Sourcepub fn decompress_data(
pack: &mut (impl BufRead + Send),
expected_size: usize,
) -> Result<(Vec<u8>, usize), GitError>
pub fn decompress_data( pack: &mut (impl BufRead + Send), expected_size: usize, ) -> Result<(Vec<u8>, usize), GitError>
Decompresses data from a given Read and BufRead source using Zlib decompression.
§Parameters
pack: A source that implements both Read and BufRead traits (e.g., file, network stream).expected_size: The expected decompressed size of the data.
§Returns
Returns a Result containing either:
- A tuple with a
Vec<u8>of the decompressed data and the total number of input bytes processed, - Or a
GitErrorin case of a mismatch in expected size or any other reading error.
Sourcepub fn decode_pack_object(
pack: &mut (impl BufRead + Send),
offset: &mut usize,
) -> Result<Option<CacheObject>, GitError>
pub fn decode_pack_object( pack: &mut (impl BufRead + Send), offset: &mut usize, ) -> Result<Option<CacheObject>, GitError>
Decodes a pack object from a given Read and BufRead source and returns the object as a CacheObject.
§Parameters
pack: A source that implements both Read and BufRead traits.offset: A mutable reference to the current offset within the pack.
§Returns
Returns a Result containing either:
- A tuple of the next offset in the pack and the original compressed data as
Vec<u8>, - Or a
GitErrorin case of any reading or decompression error.
Sourcepub fn decode_pack_object_with_kind(
kind: HashKind,
pack: &mut (impl BufRead + Send),
offset: &mut usize,
) -> Result<Option<CacheObject>, GitError>
pub fn decode_pack_object_with_kind( kind: HashKind, pack: &mut (impl BufRead + Send), offset: &mut usize, ) -> Result<Option<CacheObject>, GitError>
Like Pack::decode_pack_object, but the object ID width, ref-delta base ID and
the computed object hash all follow the explicit repository kind
(Pack::decode_pack_object is the thread-local compatibility wrapper).
Sourcepub fn decode<F, C>(
&mut self,
pack: &mut (impl BufRead + Send),
callback: F,
pack_id_callback: Option<C>,
) -> Result<(), GitError>where
F: Fn(MetaAttached<Entry, EntryMeta>) + Sync + Send + 'static,
C: FnOnce(ObjectHash) + Send + 'static,
pub fn decode<F, C>(
&mut self,
pack: &mut (impl BufRead + Send),
callback: F,
pack_id_callback: Option<C>,
) -> Result<(), GitError>where
F: Fn(MetaAttached<Entry, EntryMeta>) + Sync + Send + 'static,
C: FnOnce(ObjectHash) + Send + 'static,
Decodes a pack file from a given Read and BufRead source, for each object in the pack, it decodes the object and processes it using the provided callback function.
§Parameters
- pack_id_callback: A callback that seed pack_file sha1 for updating database
Sourcepub fn decode_without_callback<C>(
&mut self,
pack: &mut (impl BufRead + Send),
pack_id_callback: Option<C>,
) -> Result<(), GitError>
pub fn decode_without_callback<C>( &mut self, pack: &mut (impl BufRead + Send), pack_id_callback: Option<C>, ) -> Result<(), GitError>
Decodes a pack file without materializing callback entries.
Use this when the caller only needs validation, hashing, and cache/delta reconstruction side
effects. This preserves decode for callers that consume each decoded object, while avoiding
one full object-data clone per completed object on the no-callback path.
Integrity: the pack trailer, an existing .idx (magic/version, fanout, checksums, object
count) and the name of every object are verified: base objects that are not needed are
streamed through the hasher and dropped, and every delta (leaf deltas included) is rebuilt
so its idx-supplied name is checked against the rebuilt content.
Sourcepub fn decode_file<F, C>(
&mut self,
pack_path: impl AsRef<Path>,
callback: F,
pack_id_callback: Option<C>,
) -> Result<(), GitError>where
F: Fn(MetaAttached<Entry, EntryMeta>) + Sync + Send + 'static,
C: FnOnce(ObjectHash) + Send + 'static,
pub fn decode_file<F, C>(
&mut self,
pack_path: impl AsRef<Path>,
callback: F,
pack_id_callback: Option<C>,
) -> Result<(), GitError>where
F: Fn(MetaAttached<Entry, EntryMeta>) + Sync + Send + 'static,
C: FnOnce(ObjectHash) + Send + 'static,
Decodes a pack file from disk and invokes a callback for each decoded object.
File-backed callers can use this path to reuse the same index-guided retention scan as
decode_file_without_callback, while preserving the object metadata and CRC32 values needed
by callback consumers such as index generation.
Sourcepub fn decode_file_full_without_callback<C>(
&mut self,
pack_path: impl AsRef<Path>,
pack_id_callback: Option<C>,
) -> Result<(), GitError>
pub fn decode_file_full_without_callback<C>( &mut self, pack_path: impl AsRef<Path>, pack_id_callback: Option<C>, ) -> Result<(), GitError>
Decodes a pack file from disk without constructing callback entries, while still restoring every object in the pack.
Compared with decode_file, this avoids callback metadata and object-data clones. Compared
with decode_file_without_callback, it does not skip leaf objects, so benchmark callers can
measure a complete decode without producing per-object output.
Sourcepub fn decode_file_without_callback<C>(
&mut self,
pack_path: impl AsRef<Path>,
pack_id_callback: Option<C>,
) -> Result<(), GitError>
pub fn decode_file_without_callback<C>( &mut self, pack_path: impl AsRef<Path>, pack_id_callback: Option<C>, ) -> Result<(), GitError>
Decodes a pack file from disk without retaining objects after their final delta user.
This is a no-callback fast path for validation/benchmark callers. It performs a light
dependency scan first, then releases cache entries as soon as all later delta objects that
need them have acquired an Arc to the base.
Sourcepub fn decode_async(
self,
pack: impl BufRead + Send + 'static,
sender: UnboundedSender<Entry>,
) -> JoinHandle<Pack>
pub fn decode_async( self, pack: impl BufRead + Send + 'static, sender: UnboundedSender<Entry>, ) -> JoinHandle<Pack>
Decode a Pack in a new thread and send the CacheObjects while decoding.
Attention: It will consume the pack and return in a JoinHandle.
Sourcepub async fn decode_stream(
self,
stream: impl Stream<Item = Result<Bytes, Error>> + Unpin + Send + 'static,
sender: UnboundedSender<MetaAttached<Entry, EntryMeta>>,
pack_hash_send: Option<UnboundedSender<ObjectHash>>,
) -> Self
pub async fn decode_stream( self, stream: impl Stream<Item = Result<Bytes, Error>> + Unpin + Send + 'static, sender: UnboundedSender<MetaAttached<Entry, EntryMeta>>, pack_hash_send: Option<UnboundedSender<ObjectHash>>, ) -> Self
Decodes a Pack from a Stream of Bytes, and sends the Entry while decoding.
Sourcepub fn rebuild_delta(
delta_obj: CacheObject,
base_obj: Arc<CacheObject>,
) -> Result<CacheObject, GitError>
pub fn rebuild_delta( delta_obj: CacheObject, base_obj: Arc<CacheObject>, ) -> Result<CacheObject, GitError>
Reconstruct the Delta Object based on the “base object” and return the new object.
Sourcepub fn rebuild_zstdelta(
delta_obj: CacheObject,
base_obj: Arc<CacheObject>,
) -> Result<CacheObject, GitError>
pub fn rebuild_zstdelta( delta_obj: CacheObject, base_obj: Arc<CacheObject>, ) -> Result<CacheObject, GitError>
Rebuild a zstdelta on top of a resolved base object. The rebuilt object’s ID is computed with the base object’s own hash kind; a base without a resolved ID or a failing zstdelta application is an error, never a panic or a thread-local fallback.
Source§impl Pack
impl Pack
Sourcepub fn stats_pack(path: PathBuf) -> Result<PackStats, GitError>
pub fn stats_pack(path: PathBuf) -> Result<PackStats, GitError>
Scans a pack file and returns statistics about the object types it contains.
This is a lightweight read-only utility that parses the pack header and every
object header without fully reconstructing delta chains. It therefore runs
much faster than a full Pack::decode call for large packs.
§Parameters
path- Path to the.packfile on disk.
§Returns
Ok(PackStats)– breakdown of object counts by type.Err(GitError)– if the file cannot be opened or the pack header is invalid.
§Example
use std::path::PathBuf;
use git_internal::internal::pack::{Pack, decode::PackStats};
let stats = Pack::stats_pack(PathBuf::from("repo.pack")).unwrap();
println!("total={}, commits={}, blobs={}", stats.total, stats.commits, stats.blobs);Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Pack
impl !UnwindSafe for Pack
impl Freeze for Pack
impl Send for Pack
impl Sync for Pack
impl Unpin for Pack
impl UnsafeUnpin for Pack
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.