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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#![deny(unsafe_code, rust_2018_idioms)]
#![allow(missing_docs, unused)]
use crate::hash::ObjectId;
use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc};
pub use git_actor as actor;
#[cfg(feature = "git-diff")]
pub use git_diff as diff;
pub use git_features::{parallel, progress, progress::Progress};
pub use git_hash as hash;
pub use git_object as object;
pub use git_odb as odb;
#[cfg(feature = "git-protocol")]
pub use git_protocol as protocol;
pub use git_ref as refs;
pub use git_tempfile as tempfile;
#[cfg(feature = "git-traverse")]
pub use git_traverse as traverse;
#[cfg(feature = "git-url")]
pub use git_url as url;
pub mod interrupt;
#[cfg(feature = "git-traverse")]
pub mod ext;
pub mod prelude {
pub use git_features::parallel::reduce::Finalize;
pub use git_odb::{Find, FindExt, Write};
#[cfg(all(feature = "git-traverse"))]
pub use crate::ext::*;
pub use crate::reference::ReferencesExt;
}
pub mod init;
pub mod path;
pub use path::Path;
pub mod repository;
pub struct Repository {
pub refs: git_ref::file::Store,
pub odb: git_odb::linked::Store,
pub working_tree: Option<PathBuf>,
}
mod handles {
use std::{cell::RefCell, rc::Rc, sync::Arc};
use crate::{Cache, Repository};
pub struct Shared {
pub repo: Rc<Repository>,
pub cache: RefCell<Cache>,
}
pub struct SharedArc {
pub repo: Arc<Repository>,
pub cache: RefCell<Cache>,
}
impl Clone for Shared {
fn clone(&self) -> Self {
Shared {
repo: Rc::clone(&self.repo),
cache: RefCell::new(Default::default()),
}
}
}
impl Clone for SharedArc {
fn clone(&self) -> Self {
SharedArc {
repo: Arc::clone(&self.repo),
cache: RefCell::new(Default::default()),
}
}
}
impl From<Repository> for Shared {
fn from(repo: Repository) -> Self {
Shared {
repo: Rc::new(repo),
cache: RefCell::new(Default::default()),
}
}
}
impl From<Repository> for SharedArc {
fn from(repo: Repository) -> Self {
SharedArc {
repo: Arc::new(repo),
cache: RefCell::new(Default::default()),
}
}
}
impl Repository {
pub fn into_shared(self) -> Shared {
self.into()
}
pub fn into_shared_arc(self) -> SharedArc {
self.into()
}
}
}
pub use handles::{Shared, SharedArc};
#[derive(Default)]
pub struct Cache {
packed_refs: Option<refs::packed::Buffer>,
pub pack: odb::pack::cache::Never,
pub buf: Vec<u8>,
}
mod traits {
use std::cell::RefMut;
use crate::{Cache, Repository, Shared, SharedArc};
pub trait Access {
fn repo(&self) -> &Repository;
fn cache_mut(&self) -> RefMut<'_, Cache>;
}
impl Access for Shared {
fn repo(&self) -> &Repository {
self.repo.as_ref()
}
fn cache_mut(&self) -> RefMut<'_, Cache> {
self.cache.borrow_mut()
}
}
impl Access for SharedArc {
fn repo(&self) -> &Repository {
self.repo.as_ref()
}
fn cache_mut(&self) -> RefMut<'_, Cache> {
self.cache.borrow_mut()
}
}
}
pub use traits::Access;
mod cache {
use crate::{
refs::{file, packed},
Cache,
};
impl Cache {
pub fn packed_refs(
&mut self,
file: &file::Store,
) -> Result<Option<&packed::Buffer>, packed::buffer::open::Error> {
match self.packed_refs {
Some(ref packed) => Ok(Some(packed)),
None => {
self.packed_refs = file.packed()?;
Ok(self.packed_refs.as_ref())
}
}
}
}
}
pub struct Object<'r, A> {
id: ObjectId,
access: &'r A,
}
pub struct Reference<'r, A> {
pub(crate) backing: Option<reference::Backing>,
pub(crate) access: &'r A,
}
pub mod reference {
use std::cell::RefCell;
use git_hash::ObjectId;
use crate::{refs, refs::mutable, Access, Object, Reference, Repository};
pub(crate) enum Backing {
OwnedPacked {
name: mutable::FullName,
target: ObjectId,
object: Option<ObjectId>,
},
LooseFile(refs::file::loose::Reference),
}
pub mod peel_to_id_in_place {
use quick_error::quick_error;
use crate::refs;
quick_error! {
#[derive(Debug)]
pub enum Error {
LoosePeelToId(err: refs::file::loose::reference::peel::to_id::Error) {
display("Could not peel loose reference")
from()
source(err)
}
PackedRefsOpen(err: refs::packed::buffer::open::Error) {
display("The packed-refs file could not be opened")
from()
source(err)
}
}
}
}
impl<'r, A> Reference<'r, A>
where
A: Access + Sized,
{
pub fn peel_to_id_in_place(&mut self) -> Result<Object<'r, A>, peel_to_id_in_place::Error> {
let repo = self.access.repo();
match &mut self.backing.take().expect("a ref must be set") {
Backing::LooseFile(r) => {
let oid = r.peel_to_id_in_place(
&repo.refs,
self.access.cache_mut().packed_refs(&repo.refs)?,
|oid, buf| {
repo.odb
.find(oid, buf, &mut self.access.cache_mut().pack)
.map(|po| po.map(|o| (o.kind, o.data)))
},
)?;
todo!("loose peeling")
}
Backing::OwnedPacked { name, target, object } => {
todo!("packed peeling")
}
}
}
}
mod access {
use std::{cell::RefCell, convert::TryInto};
use crate::{
hash::ObjectId,
reference::Backing,
refs,
refs::{file::find::Error, PartialName},
Access, Reference, Repository,
};
pub trait ReferencesExt: Access + Sized {
fn find_reference<'a, Name, E>(
&self,
name: Name,
) -> Result<Option<Reference<'_, Self>>, crate::reference::find::Error>
where
Name: TryInto<PartialName<'a>, Error = E>,
Error: From<E>,
{
match self
.repo()
.refs
.find(name, self.cache_mut().packed_refs(&self.repo().refs)?)
{
Ok(r) => match r {
Some(r) => Ok(Some(Reference {
backing: match r {
refs::file::Reference::Packed(p) => Backing::OwnedPacked {
name: p.name.into(),
target: p.target(),
object: p
.object
.map(|hex| ObjectId::from_hex(hex).expect("a hash kind we know")),
},
refs::file::Reference::Loose(l) => Backing::LooseFile(l),
}
.into(),
access: self,
})),
None => Ok(None),
},
Err(err) => Err(err.into()),
}
}
}
impl<A> ReferencesExt for A where A: Access + Sized {}
}
use crate::odb::Find;
pub use access::ReferencesExt;
pub mod find {
use quick_error::quick_error;
use crate::refs;
quick_error! {
#[derive(Debug)]
pub enum Error {
Find(err: refs::file::find::Error) {
display("An error occurred when trying to find a reference")
from()
source(err)
}
PackedRefsOpen(err: refs::packed::buffer::open::Error) {
display("The packed-refs file could not be opened")
from()
source(err)
}
}
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Kind {
Bare,
WorkingTree,
}
impl Kind {
pub fn is_bare(&self) -> bool {
matches!(self, Kind::Bare)
}
}
pub fn discover(directory: impl AsRef<std::path::Path>) -> Result<Repository, repository::discover::Error> {
Repository::discover(directory)
}