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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use crate::data;
pub trait Find {
type Error: std::error::Error + 'static;
fn find<'a>(
&self,
id: impl AsRef<git_hash::oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl crate::cache::DecodeEntry,
) -> Result<Option<data::Object<'a>>, Self::Error>;
fn location_by_oid(&self, id: impl AsRef<git_hash::oid>, buf: &mut Vec<u8>) -> Option<crate::bundle::Location>;
fn bundle_by_pack_id(&self, pack_id: u32) -> Option<&crate::Bundle>;
fn entry_by_location(&self, location: &crate::bundle::Location) -> Option<Entry<'_>>;
}
mod ext {
use crate::{data, find};
use git_object::{immutable, Kind};
macro_rules! make_obj_lookup {
($method:ident, $object_variant:path, $object_kind:path, $object_type:ty) => {
fn $method<'a>(
&self,
id: impl AsRef<git_hash::oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl crate::cache::DecodeEntry,
) -> Result<$object_type, find::existing_object::Error<Self::Error>> {
let id = id.as_ref();
self.find(id, buffer, pack_cache)
.map_err(find::existing_object::Error::Find)?
.ok_or_else(|| find::existing_object::Error::NotFound {
oid: id.as_ref().to_owned(),
})
.and_then(|o| o.decode().map_err(find::existing_object::Error::Decode))
.and_then(|o| match o {
$object_variant(o) => return Ok(o),
_other => Err(find::existing_object::Error::ObjectKind {
expected: $object_kind,
}),
})
}
};
}
macro_rules! make_iter_lookup {
($method:ident, $object_kind:path, $object_type:ty, $into_iter:tt) => {
fn $method<'a>(
&self,
id: impl AsRef<git_hash::oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl crate::cache::DecodeEntry,
) -> Result<$object_type, find::existing_iter::Error<Self::Error>> {
let id = id.as_ref();
self.find(id, buffer, pack_cache)
.map_err(find::existing_iter::Error::Find)?
.ok_or_else(|| find::existing_iter::Error::NotFound {
oid: id.as_ref().to_owned(),
})
.and_then(|o| {
o.$into_iter()
.ok_or_else(|| find::existing_iter::Error::ObjectKind {
expected: $object_kind,
})
})
}
};
}
pub trait FindExt: super::Find {
fn find_existing<'a>(
&self,
id: impl AsRef<git_hash::oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl crate::cache::DecodeEntry,
) -> Result<data::Object<'a>, find::existing::Error<Self::Error>> {
let id = id.as_ref();
self.find(id, buffer, pack_cache)
.map_err(find::existing::Error::Find)?
.ok_or_else(|| find::existing::Error::NotFound {
oid: id.as_ref().to_owned(),
})
}
make_obj_lookup!(
find_existing_commit,
immutable::Object::Commit,
Kind::Commit,
immutable::Commit<'a>
);
make_obj_lookup!(
find_existing_tree,
immutable::Object::Tree,
Kind::Tree,
immutable::Tree<'a>
);
make_obj_lookup!(find_existing_tag, immutable::Object::Tag, Kind::Tag, immutable::Tag<'a>);
make_obj_lookup!(
find_existing_blob,
immutable::Object::Blob,
Kind::Blob,
immutable::Blob<'a>
);
make_iter_lookup!(
find_existing_commit_iter,
Kind::Blob,
immutable::CommitIter<'a>,
into_commit_iter
);
make_iter_lookup!(
find_existing_tree_iter,
Kind::Tree,
immutable::TreeIter<'a>,
into_tree_iter
);
}
impl<T: super::Find> FindExt for T {}
}
pub use ext::FindExt;
pub mod existing {
use git_hash::ObjectId;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error<T: std::error::Error + 'static> {
#[error(transparent)]
Find(T),
#[error("An object with id {} could not be found", .oid)]
NotFound { oid: ObjectId },
}
}
pub mod existing_object {
use git_hash::ObjectId;
use git_object::immutable;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error<T: std::error::Error + 'static> {
#[error(transparent)]
Find(T),
#[error(transparent)]
Decode(immutable::object::decode::Error),
#[error("An object with id {} could not be found", .oid)]
NotFound { oid: ObjectId },
#[error("Expected object of kind {} something else", .expected)]
ObjectKind { expected: git_object::Kind },
}
}
pub mod existing_iter {
use git_hash::ObjectId;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error<T: std::error::Error + 'static> {
#[error(transparent)]
Find(T),
#[error("An object with id {} could not be found", .oid)]
NotFound { oid: ObjectId },
#[error("Expected object of kind {} something else", .expected)]
ObjectKind { expected: git_object::Kind },
}
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub struct Entry<'a> {
pub data: &'a [u8],
pub crc32: Option<u32>,
pub version: crate::data::Version,
}
mod find_impls {
use crate::bundle::Location;
use crate::{data::Object, find::Entry, Bundle};
use git_hash::oid;
use std::ops::Deref;
impl<T> super::Find for std::sync::Arc<T>
where
T: super::Find,
{
type Error = T::Error;
fn find<'a>(
&self,
id: impl AsRef<oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl crate::cache::DecodeEntry,
) -> Result<Option<Object<'a>>, Self::Error> {
self.deref().find(id, buffer, pack_cache)
}
fn location_by_oid(&self, id: impl AsRef<oid>, buf: &mut Vec<u8>) -> Option<Location> {
self.deref().location_by_oid(id, buf)
}
fn bundle_by_pack_id(&self, pack_id: u32) -> Option<&Bundle> {
self.deref().bundle_by_pack_id(pack_id)
}
fn entry_by_location(&self, object: &crate::bundle::Location) -> Option<Entry<'_>> {
self.deref().entry_by_location(object)
}
}
impl<T> super::Find for Box<T>
where
T: super::Find,
{
type Error = T::Error;
fn find<'a>(
&self,
id: impl AsRef<oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl crate::cache::DecodeEntry,
) -> Result<Option<Object<'a>>, Self::Error> {
self.deref().find(id, buffer, pack_cache)
}
fn location_by_oid(&self, id: impl AsRef<oid>, buf: &mut Vec<u8>) -> Option<Location> {
self.deref().location_by_oid(id, buf)
}
fn bundle_by_pack_id(&self, pack_id: u32) -> Option<&Bundle> {
self.deref().bundle_by_pack_id(pack_id)
}
fn entry_by_location(&self, location: &crate::bundle::Location) -> Option<Entry<'_>> {
self.deref().entry_by_location(location)
}
}
}