git_pack/index/
mod.rs

1//! an index into the pack file
2//!
3/// From itertools
4/// Create an iterator running multiple iterators in lockstep.
5///
6/// The `izip!` iterator yields elements until any subiterator
7/// returns `None`.
8///
9/// This is a version of the standard ``.zip()`` that's supporting more than
10/// two iterators. The iterator element type is a tuple with one element
11/// from each of the input iterators. Just like ``.zip()``, the iteration stops
12/// when the shortest of the inputs reaches its end.
13///
14/// **Note:** The result of this macro is in the general case an iterator
15/// composed of repeated `.zip()` and a `.map()`; it has an anonymous type.
16/// The special cases of one and two arguments produce the equivalent of
17/// `$a.into_iter()` and `$a.into_iter().zip($b)` respectively.
18///
19/// Prefer this macro `izip!()` over [`multizip`] for the performance benefits
20/// of using the standard library `.zip()`.
21///
22/// [`multizip`]: fn.multizip.html
23///
24/// ```
25/// # use itertools::izip;
26/// #
27/// # fn main() {
28///
29/// // iterate over three sequences side-by-side
30/// let mut results = [0, 0, 0, 0];
31/// let inputs = [3, 7, 9, 6];
32///
33/// for (r, index, input) in izip!(&mut results, 0..10, &inputs) {
34///     *r = index * 10 + input;
35/// }
36///
37/// assert_eq!(results, [0 + 3, 10 + 7, 29, 36]);
38/// # }
39/// ```
40macro_rules! izip {
41    // @closure creates a tuple-flattening closure for .map() call. usage:
42    // @closure partial_pattern => partial_tuple , rest , of , iterators
43    // eg. izip!( @closure ((a, b), c) => (a, b, c) , dd , ee )
44    ( @closure $p:pat => $tup:expr ) => {
45        |$p| $tup
46    };
47
48    // The "b" identifier is a different identifier on each recursion level thanks to hygiene.
49    ( @closure $p:pat => ( $($tup:tt)* ) , $_iter:expr $( , $tail:expr )* ) => {
50        izip!(@closure ($p, b) => ( $($tup)*, b ) $( , $tail )*)
51    };
52
53    // unary
54    ($first:expr $(,)*) => {
55        std::iter::IntoIterator::into_iter($first)
56    };
57
58    // binary
59    ($first:expr, $second:expr $(,)*) => {
60        izip!($first)
61            .zip($second)
62    };
63
64    // n-ary where n > 2
65    ( $first:expr $( , $rest:expr )* $(,)* ) => {
66        izip!($first)
67            $(
68                .zip($rest)
69            )*
70            .map(
71                izip!(@closure a => (a) $( , $rest )*)
72            )
73    };
74}
75
76use memmap2::Mmap;
77
78/// The version of an index file
79#[derive(PartialEq, Eq, Ord, PartialOrd, Debug, Hash, Clone, Copy)]
80#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
81#[allow(missing_docs)]
82pub enum Version {
83    V1 = 1,
84    V2 = 2,
85}
86
87impl Default for Version {
88    fn default() -> Self {
89        Version::V2
90    }
91}
92
93impl Version {
94    /// The kind of hash to produce to be compatible to this kind of index
95    pub fn hash(&self) -> git_hash::Kind {
96        match self {
97            Version::V1 | Version::V2 => git_hash::Kind::Sha1,
98        }
99    }
100}
101
102/// A way to indicate if a lookup, despite successful, was ambiguous or yielded exactly
103/// one result in the particular index.
104pub type PrefixLookupResult = Result<EntryIndex, ()>;
105
106/// The type for referring to indices of an entry within the index file.
107pub type EntryIndex = u32;
108
109const FAN_LEN: usize = 256;
110
111/// A representation of a pack index file
112pub struct File {
113    data: Mmap,
114    path: std::path::PathBuf,
115    version: Version,
116    num_objects: u32,
117    fan: [u32; FAN_LEN],
118    hash_len: usize,
119    object_hash: git_hash::Kind,
120}
121
122/// Basic file information
123impl File {
124    /// The version of the pack index
125    pub fn version(&self) -> Version {
126        self.version
127    }
128    /// The path of the opened index file
129    pub fn path(&self) -> &std::path::Path {
130        &self.path
131    }
132    /// The amount of objects stored in the pack and index, as one past the highest entry index.
133    pub fn num_objects(&self) -> EntryIndex {
134        self.num_objects
135    }
136    /// The kind of hash we assume
137    pub fn object_hash(&self) -> git_hash::Kind {
138        self.object_hash
139    }
140}
141
142const V2_SIGNATURE: &[u8] = b"\xfftOc";
143///
144pub mod init;
145
146pub(crate) mod access;
147pub use access::Entry;
148
149///
150pub mod traverse;
151mod util;
152///
153pub mod verify;
154///
155pub mod write;