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
use std::{
convert::TryInto,
io::{self, Read},
path::{Path, PathBuf},
};
pub use error::Error;
use crate::{
file,
store_impl::{
file::{loose, path_to_name},
packed,
},
FullName, PartialNameRef, Reference,
};
enum Transform {
EnforceRefsPrefix,
None,
}
impl file::Store {
pub fn try_find<'a, Name, E>(&self, partial: Name) -> Result<Option<Reference>, Error>
where
Name: TryInto<PartialNameRef<'a>, Error = E>,
Error: From<E>,
{
let path = partial.try_into()?;
let packed = self.assure_packed_refs_uptodate()?;
self.find_one_with_verified_input(path.to_partial_path().as_ref(), packed.as_deref())
}
pub fn try_find_loose<'a, Name, E>(&self, partial: Name) -> Result<Option<loose::Reference>, Error>
where
Name: TryInto<PartialNameRef<'a>, Error = E>,
Error: From<E>,
{
let path = partial.try_into()?;
self.find_one_with_verified_input(path.to_partial_path().as_ref(), None)
.map(|r| r.map(|r| r.try_into().expect("only loose refs are found without pack")))
}
pub fn try_find_packed<'a, Name, E>(
&self,
partial: Name,
packed: Option<&packed::Buffer>,
) -> Result<Option<Reference>, Error>
where
Name: TryInto<PartialNameRef<'a>, Error = E>,
Error: From<E>,
{
let path = partial.try_into()?;
self.find_one_with_verified_input(path.to_partial_path().as_ref(), packed)
}
pub(crate) fn find_one_with_verified_input(
&self,
relative_path: &Path,
packed: Option<&packed::Buffer>,
) -> Result<Option<Reference>, Error> {
let is_all_uppercase = relative_path
.to_string_lossy()
.as_ref()
.chars()
.all(|c| c.is_ascii_uppercase());
if relative_path.components().count() == 1 && is_all_uppercase {
if let Some(r) = self.find_inner("", relative_path, None, Transform::None)? {
return Ok(Some(r));
}
}
for inbetween in &["", "tags", "heads", "remotes"] {
match self.find_inner(*inbetween, relative_path, packed, Transform::EnforceRefsPrefix) {
Ok(Some(r)) => return Ok(Some(r)),
Ok(None) => {
continue;
}
Err(err) => return Err(err),
}
}
self.find_inner(
"remotes",
&relative_path.join("HEAD"),
None,
Transform::EnforceRefsPrefix,
)
}
fn find_inner(
&self,
inbetween: &str,
relative_path: &Path,
packed: Option<&packed::Buffer>,
transform: Transform,
) -> Result<Option<Reference>, Error> {
let (base, is_definitely_absolute) = match transform {
Transform::EnforceRefsPrefix => (
if relative_path.starts_with("refs") {
PathBuf::new()
} else {
PathBuf::from("refs")
},
true,
),
Transform::None => (PathBuf::new(), false),
};
let relative_path = base.join(inbetween).join(relative_path);
let contents = match self.ref_contents(&relative_path)? {
None => {
if is_definitely_absolute {
if let Some(packed) = packed {
let full_name = path_to_name(match &self.namespace {
None => relative_path,
Some(namespace) => namespace.to_owned().into_namespaced_prefix(relative_path),
});
let full_name = PartialNameRef(full_name.into_owned().into());
if let Some(packed_ref) = packed.try_find(full_name)? {
let mut res: Reference = packed_ref.into();
if let Some(namespace) = &self.namespace {
res.strip_namespace(namespace);
}
return Ok(Some(res));
};
}
}
return Ok(None);
}
Some(c) => c,
};
Ok(Some({
let full_name = path_to_name(&relative_path);
loose::Reference::try_from_path(FullName(full_name.into_owned()), &contents)
.map(Into::into)
.map(|mut r: Reference| {
if let Some(namespace) = &self.namespace {
r.strip_namespace(namespace);
}
r
})
.map_err(|err| Error::ReferenceCreation { err, relative_path })?
}))
}
}
impl file::Store {
pub(crate) fn reference_path(&self, name: &Path) -> PathBuf {
match &self.namespace {
None => self.base.join(name),
Some(namespace) => self.base.join(namespace.to_path()).join(name),
}
}
pub(crate) fn ref_contents(&self, relative_path: &Path) -> std::io::Result<Option<Vec<u8>>> {
let mut buf = Vec::new();
let ref_path = self.reference_path(relative_path);
match std::fs::File::open(&ref_path) {
Ok(mut file) => {
if let Err(err) = file.read_to_end(&mut buf) {
return if ref_path.is_dir() { Ok(None) } else { Err(err) };
}
Ok(Some(buf))
}
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(None),
#[cfg(target_os = "windows")]
Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => Ok(None),
Err(err) => Err(err),
}
}
}
pub mod existing {
use std::convert::TryInto;
pub use error::Error;
use crate::{
file::{self},
store_impl::{
file::{find, loose},
packed,
},
PartialNameRef, Reference,
};
impl file::Store {
pub fn find<'a, Name, E>(&self, partial: Name) -> Result<Reference, Error>
where
Name: TryInto<PartialNameRef<'a>, Error = E>,
crate::name::Error: From<E>,
{
let packed = self.assure_packed_refs_uptodate().map_err(find::Error::PackedOpen)?;
self.find_existing_inner(partial, packed.as_deref())
}
pub fn find_packed<'a, Name, E>(
&self,
partial: Name,
packed: Option<&packed::Buffer>,
) -> Result<Reference, Error>
where
Name: TryInto<PartialNameRef<'a>, Error = E>,
crate::name::Error: From<E>,
{
self.find_existing_inner(partial, packed)
}
pub fn find_loose<'a, Name, E>(&self, partial: Name) -> Result<loose::Reference, Error>
where
Name: TryInto<PartialNameRef<'a>, Error = E>,
crate::name::Error: From<E>,
{
self.find_existing_inner(partial, None)
.map(|r| r.try_into().expect("always loose without packed"))
}
pub(crate) fn find_existing_inner<'a, Name, E>(
&self,
partial: Name,
packed: Option<&packed::Buffer>,
) -> Result<Reference, Error>
where
Name: TryInto<PartialNameRef<'a>, Error = E>,
crate::name::Error: From<E>,
{
let path = partial
.try_into()
.map_err(|err| Error::Find(find::Error::RefnameValidation(err.into())))?;
match self.find_one_with_verified_input(path.to_partial_path().as_ref(), packed) {
Ok(Some(r)) => Ok(r),
Ok(None) => Err(Error::NotFound(path.to_partial_path().to_owned())),
Err(err) => Err(err.into()),
}
}
}
mod error {
use std::path::PathBuf;
use quick_error::quick_error;
use crate::store_impl::file::find;
quick_error! {
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Error {
Find(err: find::Error) {
display("An error occured while trying to find a reference")
from()
source(err)
}
NotFound(name: PathBuf) {
display("The ref partially named '{}' could not be found", name.display())
}
}
}
}
}
mod error {
use std::{convert::Infallible, io, path::PathBuf};
use quick_error::quick_error;
use crate::{file, store_impl::packed};
quick_error! {
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Error {
RefnameValidation(err: crate::name::Error) {
display("The ref name or path is not a valid ref name")
from()
source(err)
}
ReadFileContents(err: io::Error) {
display("The ref file could not be read in full")
from()
source(err)
}
ReferenceCreation{ err: file::loose::reference::decode::Error, relative_path: PathBuf } {
display("The reference at '{}' could not be instantiated", relative_path.display())
source(err)
}
PackedRef(err: packed::find::Error) {
display("A packed ref lookup failed")
from()
source(err)
}
PackedOpen(err: packed::buffer::open::Error) {
display("Could not open the packed refs buffer when trying to find references.")
from()
source(err)
}
}
}
impl From<Infallible> for Error {
fn from(_: Infallible) -> Self {
unreachable!("this impl is needed to allow passing a known valid partial path as parameter")
}
}
}