-
-
Notifications
You must be signed in to change notification settings - Fork 260
/
loose.rs
325 lines (290 loc) · 11.8 KB
/
loose.rs
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use std::sync::atomic::AtomicBool;
use git_actor::{Sign, Time};
use git_features::progress;
use git_object::bstr::ByteSlice;
use git_odb::loose::Store;
use pretty_assertions::assert_eq;
use crate::{fixture_path, hex_to_id};
fn ldb() -> Store {
Store::at(fixture_path("objects"), git_hash::Kind::Sha1)
}
pub fn object_ids() -> Vec<git_hash::ObjectId> {
vec![
hex_to_id("37d4e6c5c48ba0d245164c4e10d5f41140cab980"), // blob
hex_to_id("595dfd62fc1ad283d61bb47a24e7a1f66398f84d"), // blob
hex_to_id("6ba2a0ded519f737fd5b8d5ccfb141125ef3176f"), // tree
hex_to_id("722fe60ad4f0276d5a8121970b5bb9dccdad4ef9"), // tag
hex_to_id("96ae868b3539f551c88fd5f02394d022581b11b0"), // tree
hex_to_id("a706d7cd20fc8ce71489f34b50cf01011c104193"), // blob (big)
hex_to_id("ffa700b4aca13b80cb6b98a078e7c96804f8e0ec"), // commit
]
}
#[test]
fn iter() {
let mut oids = ldb().iter().map(Result::unwrap).collect::<Vec<_>>();
oids.sort();
assert_eq!(oids, object_ids());
}
pub fn locate_oid(id: git_hash::ObjectId, buf: &mut Vec<u8>) -> git_object::Data<'_> {
ldb().try_find(id, buf).expect("read success").expect("id present")
}
#[test]
fn verify_integrity() {
let db = ldb();
let outcome = db.verify_integrity(progress::Discard, &AtomicBool::new(false)).unwrap();
assert_eq!(outcome.num_objects, 7);
}
mod write {
use git_odb::{loose, Write};
use crate::store::loose::{locate_oid, object_ids};
#[test]
fn read_and_write() -> Result<(), Box<dyn std::error::Error>> {
let dir = tempfile::tempdir()?;
let db = loose::Store::at(dir.path(), git_hash::Kind::Sha1);
let mut buf = Vec::new();
let mut buf2 = Vec::new();
for oid in object_ids() {
let obj = locate_oid(oid, &mut buf);
let actual = db.write(&obj.decode()?)?;
assert_eq!(actual, oid);
assert_eq!(
db.try_find(oid, &mut buf2)?.expect("id present").decode()?,
obj.decode()?
);
let actual = db.write_buf(obj.kind, obj.data)?;
assert_eq!(actual, oid);
assert_eq!(
db.try_find(oid, &mut buf2)?.expect("id present").decode()?,
obj.decode()?
);
}
Ok(())
}
}
mod contains {
use crate::store::loose::ldb;
#[test]
fn iterable_objects_are_contained() {
let store = ldb();
for oid in store.iter().map(Result::unwrap) {
assert!(store.contains(oid));
}
}
}
mod lookup_prefix {
use std::collections::HashSet;
use git_testtools::{fixture_path, hex_to_id};
use maplit::hashset;
use crate::store::loose::ldb;
#[test]
fn returns_none_for_prefixes_without_any_match() {
let store = ldb();
let prefix = git_hash::Prefix::new(git_hash::ObjectId::null(git_hash::Kind::Sha1), 7).unwrap();
assert!(store.lookup_prefix(prefix, None).unwrap().is_none());
let mut candidates = HashSet::default();
assert!(
store.lookup_prefix(prefix, Some(&mut candidates)).unwrap().is_none(),
"error codes are the same"
);
assert!(candidates.is_empty());
}
#[test]
fn returns_some_err_for_prefixes_with_more_than_one_match() {
let objects_dir = git_testtools::tempfile::tempdir().unwrap();
git_testtools::copy_recursively_into_existing_dir(fixture_path("objects"), &objects_dir).unwrap();
std::fs::write(
objects_dir
.path()
.join("37")
.join("d4ffffffffffffffffffffffffffffffffffff"),
b"fake",
)
.unwrap();
let store = git_odb::loose::Store::at(objects_dir.path(), git_hash::Kind::Sha1);
let input_id = hex_to_id("37d4e6c5c48ba0d245164c4e10d5f41140cab980");
let prefix = git_hash::Prefix::new(input_id, 4).unwrap();
assert_eq!(
store.lookup_prefix(prefix, None).unwrap(),
Some(Err(())),
"there are two objects with that prefix"
);
let mut candidates = HashSet::default();
assert_eq!(
store.lookup_prefix(prefix, Some(&mut candidates)).unwrap(),
Some(Err(())),
"the error code is the same"
);
assert_eq!(
candidates,
hashset! {hex_to_id("37d4ffffffffffffffffffffffffffffffffffff"), input_id},
"we get both matching objects"
);
}
#[test]
fn iterable_objects_can_be_looked_up_with_varying_prefix_lengths() {
let store = ldb();
let hex_lengths = &[4, 7, 40];
for (index, oid) in store.iter().map(Result::unwrap).enumerate() {
for mut candidates in [None, Some(HashSet::default())] {
let hex_len = hex_lengths[index % hex_lengths.len()];
let prefix = git_hash::Prefix::new(oid, hex_len).unwrap();
assert_eq!(
store
.lookup_prefix(prefix, candidates.as_mut())
.unwrap()
.expect("object exists")
.expect("unambiguous"),
oid
);
if let Some(candidates) = candidates {
assert_eq!(candidates, hashset! {oid});
}
}
}
}
}
mod find {
use git_object::{bstr::ByteSlice, tree::EntryMode, BlobRef, CommitRef, Kind, TagRef, TreeRef};
use crate::{
hex_to_id,
store::loose::{ldb, locate_oid, signature},
};
fn find<'a>(hex: &str, buf: &'a mut Vec<u8>) -> git_object::Data<'a> {
locate_oid(hex_to_id(hex), buf)
}
#[test]
fn tag() -> Result<(), Box<dyn std::error::Error>> {
let mut buf = Vec::new();
let o = find("722fe60ad4f0276d5a8121970b5bb9dccdad4ef9", &mut buf);
assert_eq!(o.kind, Kind::Tag);
assert_eq!(o.data.len(), 1024);
let expected = TagRef {
target: b"ffa700b4aca13b80cb6b98a078e7c96804f8e0ec".as_bstr(),
name: b"1.0.0".as_bstr(),
target_kind: Kind::Commit,
message: b"for the signature".as_bstr(),
pgp_signature: Some(
b"-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - https://gpgtools.org
iQIzBAABCgAdFiEEw7xSvXbiwjusbsBqZl+Z+p2ZlmwFAlsapyYACgkQZl+Z+p2Z
lmy6Ug/+KzvzqiNpzz1bMVVAzp8NCbiEO3QGYPyeQc521lBwpaTrRYR+oHJY15r3
OdL5WDysTpjN8N5FNyfmvzkuPdTkK3JlYmO7VRjdA2xu/B6vIZLaOfAowFrhMvKo
8eoqwGcAP3rC5TuWEgzq2qhbjS4JXFLd4NLjWEFqT2Y2UKm+g8TeGOsa/0pF4Nq5
xeW4qCYR0WcQLFedbpkKHxag2GfaXKvzNNJdqYhVQssNa6BeSmsfDvlWYNe617wV
NvsR/zJT0wHb5SSH+h6QmwA7LQIQF//83Vc3aF7kv9D54r3ibXW5TjZ3WoeTUZO7
kefkzJ12EYDCFLPhHvXPog518nO8Ot46dX+okrF0/B4N3RFTvjKr7VAGTzv2D/Dg
DrD531S2F71b+JIRh641eeP7bjWFQi3tWLtrEOtjjsKPJfYRMKpYFnAO4UUJ6Rck
Z5fFXEUCO8d5WT56jzKDjmVoY01lA87O1YsP/J+zQAlc9v1k6jqeQ53LZNgTN+ue
5fJuSPT3T43pSOD1VQSr3aZ2Anc4Qu7K8uX9lkpxF9Sc0tDbeCosFLZMWNVp6m+e
cjHJZXWmV4CcRfmLsXzU8s2cR9A0DBvOxhPD1TlKC2JhBFXigjuL9U4Rbq9tdegB
2n8f2douw6624Tn/6Lm4a7AoxmU+CMiYagDxDL3RuZ8CAfh3bn0=
=aIns
-----END PGP SIGNATURE-----
"
.as_bstr(),
),
tagger: Some(signature(1528473343)),
};
assert_eq!(o.decode()?.as_tag().expect("tag"), &expected);
Ok(())
}
#[test]
fn commit() -> Result<(), Box<dyn std::error::Error>> {
let mut buf = Vec::new();
let o = find("ffa700b4aca13b80cb6b98a078e7c96804f8e0ec", &mut buf);
assert_eq!(o.kind, Kind::Commit);
assert_eq!(o.data.len(), 1084);
let expected = CommitRef {
tree: b"6ba2a0ded519f737fd5b8d5ccfb141125ef3176f".as_bstr(),
parents: vec![].into(),
author: signature(1528473303),
committer: signature(1528473303),
encoding: None,
message: b"initial commit\n".as_bstr(),
extra_headers: vec![(b"gpgsig".as_bstr(), b"-----BEGIN PGP SIGNATURE-----\nComment: GPGTools - https://gpgtools.org\n\niQIzBAABCgAdFiEEw7xSvXbiwjusbsBqZl+Z+p2ZlmwFAlsaptwACgkQZl+Z+p2Z\nlmxXSQ//fj6t7aWoEKeMdFigfj6OXWPUyrRbS0N9kpJeOfA0BIOea/6Jbn8J5qh1\nYRfrySOzHPXR5Y+w4GwLiVas66qyhAbk4yeqZM0JxBjHDyPyRGhjUd3y7WjEa6bj\nP0ACAIkYZQ/Q/LDE3eubmhAwEobBH3nZbwE+/zDIG0i265bD5C0iDumVOiKkSelw\ncr6FZVw1HH+GcabFkeLRZLNGmPqGdbeBwYERqb0U1aRCzV1xLYteoKwyWcYaH8E3\n97z1rwhUO/L7o8WUEJtP3CLB0zuocslMxskf6bCeubBnRNJ0YrRmxGarxCP3vn4D\n3a/MwECnl6mnUU9t+OnfvrzLDN73rlq8iasUq6hGe7Sje7waX6b2UGpxHqwykmXg\nVimD6Ah7svJanHryfJn38DvJW/wOMqmAnSUAp+Y8W9EIe0xVntCmtMyoKuqBoY7T\nJlZ1kHJte6ELIM5JOY9Gx7D0ZCSKZJQqyjoqtl36dsomT0I78/+7QS1DP4S6XB7d\nc3BYH0JkW81p7AAFbE543ttN0Z4wKXErMFqUKnPZUIEuybtlNYV+krRdfDBWQysT\n3MBebjguVQ60oGs06PzeYBosKGQrHggAcwduLFuqXhLTJqN4UQ18RkE0vbtG3YA0\n+XtZQM13vURdfwFI5qitAGgw4EzPVrkWWzApzLCrRPEMbvP+b9A=\n=2qqN\n-----END PGP SIGNATURE-----".as_bstr().into())]
};
let object = o.decode()?;
assert_eq!(object.as_commit().expect("commit"), &expected);
Ok(())
}
#[test]
fn blob_data() -> Result<(), Box<dyn std::error::Error>> {
let mut buf = Vec::new();
let o = find("37d4e6c5c48ba0d245164c4e10d5f41140cab980", &mut buf);
assert_eq!(o.data.as_bstr(), b"hi there\n".as_bstr());
Ok(())
}
#[test]
fn blob() -> Result<(), Box<dyn std::error::Error>> {
let mut buf = Vec::new();
let o = find("37d4e6c5c48ba0d245164c4e10d5f41140cab980", &mut buf);
assert_eq!(
o.decode()?.as_blob().expect("blob"),
&BlobRef {
data: &[104, 105, 32, 116, 104, 101, 114, 101, 10]
},
"small blobs are treated similarly to other object types and are read into memory at once when the header is read"
);
Ok(())
}
#[test]
fn blob_not_existing() {
let mut buf = Vec::new();
assert_eq!(try_locate("37d4e6c5c48ba0d245164c4e10d5f41140cab989", &mut buf), None);
}
#[test]
fn blob_big() -> Result<(), Box<dyn std::error::Error>> {
let mut buf = Vec::new();
let o = find("a706d7cd20fc8ce71489f34b50cf01011c104193", &mut buf);
assert_eq!(
o.decode()?.as_blob().expect("blob").data.len(),
o.data.len(),
"erm, blobs are the same as raw data?"
);
Ok(())
}
fn try_locate<'a>(hex: &str, buf: &'a mut Vec<u8>) -> Option<git_object::Data<'a>> {
ldb().try_find(hex_to_id(hex), buf).ok().flatten()
}
pub fn as_id(id: &[u8; 20]) -> &git_hash::oid {
id.into()
}
#[test]
fn tree() -> Result<(), Box<dyn std::error::Error>> {
let mut buf = Vec::new();
let o = find("6ba2a0ded519f737fd5b8d5ccfb141125ef3176f", &mut buf);
assert_eq!(o.kind, Kind::Tree);
assert_eq!(o.data.len(), 66);
let expected = TreeRef {
entries: vec![
git_object::tree::EntryRef {
mode: EntryMode::Tree,
filename: b"dir".as_bstr(),
oid: as_id(&[
150, 174, 134, 139, 53, 57, 245, 81, 200, 143, 213, 240, 35, 148, 208, 34, 88, 27, 17, 176,
]),
},
git_object::tree::EntryRef {
mode: EntryMode::Blob,
filename: b"file.txt".as_bstr(),
oid: as_id(&[
55, 212, 230, 197, 196, 139, 160, 210, 69, 22, 76, 78, 16, 213, 244, 17, 64, 202, 185, 128,
]),
},
],
};
assert_eq!(o.decode()?.as_tree().expect("tree"), &expected);
Ok(())
}
}
fn signature(time: u32) -> git_actor::SignatureRef<'static> {
git_actor::SignatureRef {
name: b"Sebastian Thiel".as_bstr(),
email: b"byronimo@gmail.com".as_bstr(),
time: Time {
seconds_since_unix_epoch: time,
offset_in_seconds: 7200,
sign: Sign::Plus,
},
}
}