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
use std::convert::TryInto;
use git_object::bstr::{BStr, BString, ByteSlice};
use crate::{store_impl::packed, FullNameRef, PartialNameRef};
impl packed::Buffer {
pub fn try_find<'a, Name, E>(&self, name: Name) -> Result<Option<packed::Reference<'_>>, Error>
where
Name: TryInto<&'a PartialNameRef, Error = E>,
Error: From<E>,
{
let name = name.try_into()?;
let mut buf = BString::default();
for inbetween in &["", "tags", "heads", "remotes"] {
let (name, was_absolute) = if name.looks_like_full_name() {
let name = FullNameRef::new_unchecked(name.as_bstr());
let name = match transform_full_name_for_lookup(name) {
None => return Ok(None),
Some(name) => name,
};
(name, true)
} else {
let full_name = name.construct_full_name_ref(true, inbetween, &mut buf);
(full_name, false)
};
match self.try_find_full_name(name)? {
Some(r) => return Ok(Some(r)),
None if was_absolute => return Ok(None),
None => continue,
}
}
Ok(None)
}
pub(crate) fn try_find_full_name(&self, name: &FullNameRef) -> Result<Option<packed::Reference<'_>>, Error> {
match self.binary_search_by(name.as_bstr()) {
Ok(line_start) => {
return Ok(Some(
packed::decode::reference::<()>(&self.as_ref()[line_start..])
.map_err(|_| Error::Parse)?
.1,
))
}
Err((parse_failure, _)) => {
if parse_failure {
Err(Error::Parse)
} else {
Ok(None)
}
}
}
}
pub fn find<'a, Name, E>(&self, name: Name) -> Result<packed::Reference<'_>, existing::Error>
where
Name: TryInto<&'a PartialNameRef, Error = E>,
Error: From<E>,
{
match self.try_find(name) {
Ok(Some(r)) => Ok(r),
Ok(None) => Err(existing::Error::NotFound),
Err(err) => Err(existing::Error::Find(err)),
}
}
pub(in crate::store_impl::packed) fn binary_search_by(&self, full_name: &BStr) -> Result<usize, (bool, usize)> {
let a = self.as_ref();
let search_start_of_record = |ofs: usize| {
a[..ofs]
.rfind(b"\n")
.and_then(|pos| {
let candidate = pos + 1;
a.get(candidate).and_then(|b| {
if *b == b'^' {
a[..pos].rfind(b"\n").map(|pos| pos + 1)
} else {
Some(candidate)
}
})
})
.unwrap_or(0)
};
let mut encountered_parse_failure = false;
a.binary_search_by_key(&full_name.as_ref(), |b: &u8| {
let ofs = b as *const u8 as usize - a.as_ptr() as usize;
let line = &a[search_start_of_record(ofs)..];
packed::decode::reference::<()>(line)
.map(|(_rest, r)| r.name.as_bstr().as_ref())
.map_err(|err| {
encountered_parse_failure = true;
err
})
.unwrap_or(&[])
})
.map(search_start_of_record)
.map_err(|pos| (encountered_parse_failure, search_start_of_record(pos)))
}
}
mod error {
use std::convert::Infallible;
use quick_error::quick_error;
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)
}
Parse {
display("The reference could not be parsed")
}
}
}
impl From<Infallible> for Error {
fn from(_: Infallible) -> Self {
unreachable!("this impl is needed to allow passing a known valid partial path as parameter")
}
}
}
pub use error::Error;
pub mod existing {
use quick_error::quick_error;
quick_error! {
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Error {
Find(err: super::Error) {
display("The find operation failed")
from()
source(err)
}
NotFound {
display("The reference did not exist even though that was expected")
}
}
}
}
pub(crate) fn transform_full_name_for_lookup(name: &FullNameRef) -> Option<&FullNameRef> {
match name.category_and_short_name() {
Some((c, sn)) => {
use crate::Category::*;
Some(match c {
MainRef | LinkedRef { .. } => FullNameRef::new_unchecked(sn),
Tag | RemoteBranch | LocalBranch | Bisect | Rewritten | Note => name,
MainPseudoRef | PseudoRef | LinkedPseudoRef { .. } | WorktreePrivate => return None,
})
}
None => Some(name),
}
}