1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::path::{Path, PathBuf};
use crate::{
data,
multi_index::{EntryIndex, File, PackIndex, Version},
};
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Entry {
pub oid: git_hash::ObjectId,
pub pack_offset: data::Offset,
pub pack_index: PackIndex,
}
impl File {
pub fn version(&self) -> Version {
self.version
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn num_indices(&self) -> PackIndex {
self.num_indices
}
pub fn num_objects(&self) -> EntryIndex {
self.num_objects
}
pub fn object_hash(&self) -> git_hash::Kind {
self.object_hash
}
pub fn checksum(&self) -> git_hash::ObjectId {
git_hash::ObjectId::from(&self.data[self.data.len() - self.hash_len..])
}
pub fn index_names(&self) -> &[PathBuf] {
&self.index_names
}
}
impl File {
pub fn oid_at_index(&self, index: EntryIndex) -> &git_hash::oid {
debug_assert!(index < self.num_objects, "index out of bounds");
let index: usize = index as usize;
let start = self.lookup_ofs + index * self.hash_len;
git_hash::oid::from_bytes_unchecked(&self.data[start..][..self.hash_len])
}
pub fn lookup(&self, id: impl AsRef<git_hash::oid>) -> Option<EntryIndex> {
let id = id.as_ref();
let first_byte = id.first_byte() as usize;
let mut upper_bound = self.fan[first_byte];
let mut lower_bound = if first_byte != 0 { self.fan[first_byte - 1] } else { 0 };
while lower_bound < upper_bound {
let mid = (lower_bound + upper_bound) / 2;
let mid_sha = self.oid_at_index(mid);
use std::cmp::Ordering::*;
match id.cmp(mid_sha) {
Less => upper_bound = mid,
Equal => return Some(mid),
Greater => lower_bound = mid + 1,
}
}
None
}
pub fn pack_id_and_pack_offset_at_index(&self, index: EntryIndex) -> (PackIndex, data::Offset) {
const OFFSET_ENTRY_SIZE: usize = 4 + 4;
let index = index as usize;
let start = self.offsets_ofs + index * OFFSET_ENTRY_SIZE;
const HIGH_BIT: u32 = 1 << 31;
let pack_index = crate::read_u32(&self.data[start..][..4]);
let offset = &self.data[start + 4..][..4];
let ofs32 = crate::read_u32(offset);
let pack_offset = if (ofs32 & HIGH_BIT) == HIGH_BIT {
if let Some(offsets_64) = self.large_offsets_ofs {
let from = offsets_64 + (ofs32 ^ HIGH_BIT) as usize * 8;
crate::read_u64(&self.data[from..][..8])
} else {
ofs32 as u64
}
} else {
ofs32 as u64
};
(pack_index, pack_offset)
}
pub fn iter(&self) -> impl Iterator<Item = Entry> + '_ {
(0..self.num_objects).map(move |idx| {
let (pack_index, pack_offset) = self.pack_id_and_pack_offset_at_index(idx);
Entry {
oid: self.oid_at_index(idx).to_owned(),
pack_offset,
pack_index,
}
})
}
}