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
pub const NO_PARENT_IDS: [git_hash::ObjectId; 0] = [];
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
ParseTime(#[from] crate::config::time::Error),
#[error("Committer identity is not configured")]
CommitterMissing,
#[error("Author identity is not configured")]
AuthorMissing,
#[error(transparent)]
ReferenceNameValidation(#[from] git_ref::name::Error),
#[error(transparent)]
WriteObject(#[from] crate::object::write::Error),
#[error(transparent)]
ReferenceEdit(#[from] crate::reference::edit::Error),
}
pub mod describe {
use std::borrow::Cow;
use git_hash::ObjectId;
use git_hashtable::HashMap;
use git_odb::Find;
use crate::{bstr::BStr, ext::ObjectIdExt, Repository};
pub struct Resolution<'repo> {
pub outcome: git_revision::describe::Outcome<'static>,
pub id: crate::Id<'repo>,
}
impl<'repo> Resolution<'repo> {
pub fn format(self) -> Result<git_revision::describe::Format<'static>, Error> {
let prefix = self.id.shorten()?;
Ok(self.outcome.into_format(prefix.hex_len()))
}
}
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
Describe(#[from] git_revision::describe::Error<git_odb::store::find::Error>),
#[error("Could not produce an unambiguous shortened id for formatting.")]
ShortId(#[from] crate::id::shorten::Error),
#[error(transparent)]
RefIter(#[from] crate::reference::iter::Error),
#[error(transparent)]
RefIterInit(#[from] crate::reference::iter::init::Error),
}
#[derive(Debug, Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
pub enum SelectRef {
AnnotatedTags,
AllTags,
AllRefs,
}
impl SelectRef {
fn names(&self, repo: &Repository) -> Result<HashMap<ObjectId, Cow<'static, BStr>>, Error> {
let platform = repo.references()?;
Ok(match self {
SelectRef::AllTags | SelectRef::AllRefs => {
let mut refs: Vec<_> = match self {
SelectRef::AllRefs => platform.all()?,
SelectRef::AllTags => platform.tags()?,
_ => unreachable!(),
}
.filter_map(Result::ok)
.filter_map(|mut r: crate::Reference<'_>| {
let target_id = r.target().try_id().map(ToOwned::to_owned);
let peeled_id = r.peel_to_id_in_place().ok()?;
let (prio, tag_time) = match target_id {
Some(target_id) if peeled_id != *target_id => {
let tag = repo.find_object(target_id).ok()?.try_into_tag().ok()?;
(1, tag.tagger().ok()??.time.seconds_since_unix_epoch)
}
_ => (0, 0),
};
(
peeled_id.inner,
prio,
tag_time,
Cow::from(r.inner.name.shorten().to_owned()),
)
.into()
})
.collect();
refs.sort_by(
|(_a_peeled_id, a_prio, a_time, a_name), (_b_peeled_id, b_prio, b_time, b_name)| {
a_prio
.cmp(b_prio)
.then_with(|| a_time.cmp(b_time))
.then_with(|| b_name.cmp(a_name))
},
);
refs.into_iter().map(|(a, _, _, b)| (a, b)).collect()
}
SelectRef::AnnotatedTags => {
let mut peeled_commits_and_tag_date: Vec<_> = platform
.tags()?
.filter_map(Result::ok)
.filter_map(|r: crate::Reference<'_>| {
let tag = r.try_id()?.object().ok()?.try_into_tag().ok()?;
let tag_time = tag
.tagger()
.ok()
.and_then(|s| s.map(|s| s.time.seconds_since_unix_epoch))
.unwrap_or(0);
let commit_id = tag.target_id().ok()?.object().ok()?.try_into_commit().ok()?.id;
Some((commit_id, tag_time, Cow::<BStr>::from(r.name().shorten().to_owned())))
})
.collect();
peeled_commits_and_tag_date.sort_by(|(_a_id, a_time, a_name), (_b_id, b_time, b_name)| {
a_time.cmp(b_time).then_with(|| b_name.cmp(a_name))
});
peeled_commits_and_tag_date
.into_iter()
.map(|(a, _, c)| (a, c))
.collect()
}
})
}
}
impl Default for SelectRef {
fn default() -> Self {
SelectRef::AnnotatedTags
}
}
pub struct Platform<'repo> {
pub(crate) id: git_hash::ObjectId,
pub(crate) repo: &'repo crate::Repository,
pub(crate) select: SelectRef,
pub(crate) first_parent: bool,
pub(crate) id_as_fallback: bool,
pub(crate) max_candidates: usize,
}
impl<'repo> Platform<'repo> {
pub fn names(mut self, select: SelectRef) -> Self {
self.select = select;
self
}
pub fn traverse_first_parent(mut self, first_parent: bool) -> Self {
self.first_parent = first_parent;
self
}
pub fn max_candidates(mut self, candidates: usize) -> Self {
self.max_candidates = candidates;
self
}
pub fn id_as_fallback(mut self, use_fallback: bool) -> Self {
self.id_as_fallback = use_fallback;
self
}
pub fn try_format(&self) -> Result<Option<git_revision::describe::Format<'static>>, Error> {
self.try_resolve()?.map(|r| r.format()).transpose()
}
pub fn try_resolve(&self) -> Result<Option<Resolution<'repo>>, Error> {
let outcome = git_revision::describe(
&self.id,
|id, buf| {
Ok(self
.repo
.objects
.try_find(id, buf)?
.and_then(|d| d.try_into_commit_iter()))
},
git_revision::describe::Options {
name_by_oid: self.select.names(self.repo)?,
fallback_to_oid: self.id_as_fallback,
first_parent: self.first_parent,
max_candidates: self.max_candidates,
},
)?;
Ok(outcome.map(|outcome| crate::commit::describe::Resolution {
outcome,
id: self.id.attach(self.repo),
}))
}
pub fn format(&mut self) -> Result<git_revision::describe::Format<'static>, Error> {
self.id_as_fallback = true;
Ok(self.try_format()?.expect("BUG: fallback must always produce a format"))
}
}
}