gix_pack/lib.rs
1//! Git stores all of its data as _Objects_, which are data along with a hash over all data. Storing objects efficiently
2//! is what git packs are concerned about.
3//!
4//! Packs consist of [data files][data::File] and [index files][index::File]. The latter can be generated from a data file
5//! and make accessing objects within a pack feasible.
6//!
7//! A [Bundle] conveniently combines a data pack alongside its index to allow [finding][Find] objects or verifying the pack.
8//! Objects returned by `.find(…)` are [objects][gix_object::Data] which know their pack location in order to speed up
9//! various common operations like creating new packs from existing ones.
10//!
11//! When traversing all objects in a pack, a _delta tree acceleration structure_ can be built from pack data or an index
12//! in order to decompress packs in parallel and without any waste.
13//! ## Feature Flags
14#![cfg_attr(
15 all(doc, feature = "document-features"),
16 doc = ::document_features::document_features!()
17)]
18#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg))]
19#![deny(missing_docs, unsafe_code)]
20
21use std::{borrow::Cow, ops::Deref, path::Path};
22
23/// The default memory-backed storage for pack data and index files.
24pub use memmap2::Mmap as MMap;
25
26/// A byte-oriented backing store for pack data and indices.
27pub trait FileData: Deref<Target = [u8]> {}
28
29impl<T> FileData for T where T: Deref<Target = [u8]> {}
30
31///
32pub mod bundle;
33/// A bundle of pack data and the corresponding pack index
34pub struct Bundle {
35 /// The pack file corresponding to `index`
36 pub pack: data::File,
37 /// The index file corresponding to `pack`
38 pub index: index::File,
39}
40
41///
42pub mod find;
43
44///
45pub mod cache;
46///
47pub mod data;
48
49mod find_traits;
50pub use find_traits::{Find, FindExt};
51
52///
53pub mod index;
54///
55pub mod multi_index;
56
57///
58pub mod verify;
59
60///
61#[cfg(feature = "testing")]
62pub mod testing;
63
64mod mmap {
65 use std::path::Path;
66
67 pub fn read_only(path: &Path) -> std::io::Result<memmap2::Mmap> {
68 let file = std::fs::File::open(path)?;
69 // SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file.
70 #[expect(unsafe_code)]
71 unsafe {
72 // On Windows, a copy-on-write view is charged against the system commit limit in full when it is
73 // created, so mapping a pack larger than the available commit fails with `ERROR_COMMITMENT_LIMIT`
74 // (os error 1455) even though no page is ever written. A plain read-only view costs no commit.
75 #[cfg(windows)]
76 {
77 memmap2::MmapOptions::new().map(&file)
78 }
79 // Everywhere else, `MAP_PRIVATE` is free and works under more circumstances. And in fact, it's
80 // needed for best compatibilty and won't cause troble on *nix systems.
81 #[cfg(not(windows))]
82 {
83 memmap2::MmapOptions::new().map_copy_read_only(&file)
84 }
85 }
86 }
87}
88
89/// Return a display-friendly name for pack- or index-related progress messages.
90///
91/// Prefer the file name, but fall back to the full path for paths without a terminal component.
92fn source_name(path: &Path) -> Cow<'_, str> {
93 if path.as_os_str().is_empty() {
94 Cow::Borrowed("<memory>")
95 } else if let Some(name) = path.file_name() {
96 name.to_string_lossy()
97 } else {
98 path.as_os_str().to_string_lossy()
99 }
100}
101
102#[inline]
103fn read_u32(b: &[u8]) -> u32 {
104 u32::from_be_bytes(b.try_into().unwrap())
105}
106
107#[inline]
108fn read_u64(b: &[u8]) -> u64 {
109 u64::from_be_bytes(b.try_into().unwrap())
110}
111
112fn exact_vec<T>(capacity: usize) -> Vec<T> {
113 let mut v = Vec::new();
114 _ = v.try_reserve_exact(capacity);
115 v
116}
117
118#[inline]
119fn fan_is_monotonically_increasing(fan: &[u32]) -> bool {
120 !fan.windows(2).any(|window| window[0] > window[1])
121}
122
123#[cfg(test)]
124mod tests {
125 #[test]
126 fn exact_capacity_is_only_an_optimization() {
127 let result = std::panic::catch_unwind(|| super::exact_vec::<u8>(usize::MAX));
128 assert!(result.is_ok(), "failure to preallocate an optimization must not panic");
129 assert_eq!(
130 result.expect("preallocation does not panic").capacity(),
131 0,
132 "a failed preallocation leaves the vector empty"
133 );
134 }
135}