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
|
/// An iterator over the ancestors one or more starting commits
pub struct Ancestors<Find, Predicate, StateMut> {
find: Find,
predicate: Predicate,
state: StateMut,
parents: Parents,
sorting: Sorting,
}
/// Specify how to handle commit parents during traversal.
#[derive(Default, Copy, Clone)]
pub enum Parents {
/// Traverse all parents, useful for traversing the entire ancestry.
#[default]
All,
/// Only traverse along the first parent, which commonly ignores all branches.
First,
}
/// Specify how to sort commits during traversal.
#[derive(Default, Debug, Copy, Clone)]
pub enum Sorting {
/// Commits are sorted as they are mentioned in the commit graph.
#[default]
Topological,
/// Commits are sorted by their commit time in descending order, that is newest first.
///
/// The sorting applies to all currently queued commit ids and thus is full.
///
/// # Performance
///
/// This mode benefits greatly from having an object_cache in `find()`
/// to avoid having to lookup each commit twice.
ByCommitTimeNewestFirst,
/// This sorting is similar to `ByCommitTimeNewestFirst`, but adds a cutoff to not return commits older than
/// a given time, stopping the iteration once no younger commits is queued to be traversed.
///
/// As the query is usually repeated with different cutoff dates, this search mode benefits greatly from an object cache.
ByCommitTimeNewestFirstCutoffOlderThan {
/// The amount of seconds since unix epoch, the same value obtained by any `gix_date::Time` structure and the way git counts time.
time_in_seconds_since_epoch: u32,
},
}
///
pub mod ancestors {
use std::{
borrow::{Borrow, BorrowMut},
collections::VecDeque,
iter::FromIterator,
};
use gix_hash::{oid, ObjectId};
use gix_hashtable::HashSet;
use gix_object::CommitRefIter;
use crate::commit::{Ancestors, Parents, Sorting};
/// The error is part of the item returned by the [Ancestors] iterator.
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("The commit {oid} could not be found")]
FindExisting {
oid: ObjectId,
source: Box<dyn std::error::Error + Send + Sync + 'static>,
},
#[error(transparent)]
ObjectDecode(#[from] gix_object::decode::Error),
}
type TimeInSeconds = u32;
/// The state used and potentially shared by multiple graph traversals.
#[derive(Default, Clone)]
pub struct State {
next: VecDeque<(ObjectId, TimeInSeconds)>,
buf: Vec<u8>,
seen: HashSet<ObjectId>,
parents_buf: Vec<u8>,
}
impl State {
fn clear(&mut self) {
self.next.clear();
self.buf.clear();
self.seen.clear();
}
}
/// Builder
impl<Find, Predicate, StateMut> Ancestors<Find, Predicate, StateMut> {
/// Change our commit parent handling mode to the given one.
pub fn parents(mut self, mode: Parents) -> Self {
self.parents = mode;
self
}
}
/// Builder
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
StateMut: BorrowMut<State>,
E: std::error::Error + Send + Sync + 'static,
{
/// Set the sorting method, either topological or by author date
pub fn sorting(mut self, sorting: Sorting) -> Result<Self, Error> {
self.sorting = sorting;
if !matches!(self.sorting, Sorting::Topological) {
let mut cutoff_time_storage = self.sorting.cutoff_time().map(|cot| (cot, Vec::new()));
let state = self.state.borrow_mut();
for (commit_id, commit_time) in state.next.iter_mut() {
let commit_iter = (self.find)(commit_id, &mut state.buf).map_err(|err| Error::FindExisting {
oid: *commit_id,
source: err.into(),
})?;
let time = commit_iter.committer()?.time.seconds_since_unix_epoch;
match &mut cutoff_time_storage {
Some((cutoff_time, storage)) if time >= *cutoff_time => {
storage.push((*commit_id, time));
}
Some(_) => {}
None => *commit_time = time,
}
}
let mut v = match cutoff_time_storage {
Some((_, storage)) => storage,
None => Vec::from_iter(std::mem::take(&mut state.next).into_iter()),
};
v.sort_by(|a, b| a.1.cmp(&b.1).reverse());
state.next = v.into();
}
Ok(self)
}
}
/// Initialization
impl<Find, StateMut, E> Ancestors<Find, fn(&oid) -> bool, StateMut>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
StateMut: BorrowMut<State>,
E: std::error::Error + Send + Sync + 'static,
{
/// Create a new instance.
///
/// * `find` - a way to lookup new object data during traversal by their ObjectId, writing their data into buffer and returning
/// an iterator over commit tokens if the object is present and is a commit. Caching should be implemented within this function
/// as needed.
/// * `state` - all state used for the traversal. If multiple traversals are performed, allocations can be minimized by reusing
/// this state.
/// * `tips`
/// * the starting points of the iteration, usually commits
/// * each commit they lead to will only be returned once, including the tip that started it
pub fn new(tips: impl IntoIterator<Item = impl Into<ObjectId>>, state: StateMut, find: Find) -> Self {
Self::filtered(tips, state, find, |_| true)
}
}
/// Initialization
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
Predicate: FnMut(&oid) -> bool,
StateMut: BorrowMut<State>,
E: std::error::Error + Send + Sync + 'static,
{
/// Create a new instance with commit filtering enabled.
///
/// * `find` - a way to lookup new object data during traversal by their ObjectId, writing their data into buffer and returning
/// an iterator over commit tokens if the object is present and is a commit. Caching should be implemented within this function
/// as needed.
/// * `state` - all state used for the traversal. If multiple traversals are performed, allocations can be minimized by reusing
/// this state.
/// * `tips`
/// * the starting points of the iteration, usually commits
/// * each commit they lead to will only be returned once, including the tip that started it
/// * `predicate` - indicate whether a given commit should be included in the result as well
/// as whether its parent commits should be traversed.
pub fn filtered(
tips: impl IntoIterator<Item = impl Into<ObjectId>>,
mut state: StateMut,
find: Find,
mut predicate: Predicate,
) -> Self {
let tips = tips.into_iter();
{
let state = state.borrow_mut();
state.clear();
state.next.reserve(tips.size_hint().0);
for tip in tips.map(Into::into) {
let was_inserted = state.seen.insert(tip);
if was_inserted && predicate(&tip) {
state.next.push_back((tip, 0));
}
}
}
Self {
find,
predicate,
state,
parents: Default::default(),
sorting: Default::default(),
}
}
}
/// Access
impl<Find, Predicate, StateMut> Ancestors<Find, Predicate, StateMut>
where
StateMut: Borrow<State>,
{
/// Return an iterator for accessing more of the current commits data.
pub fn commit_iter(&self) -> CommitRefIter<'_> {
CommitRefIter::from_bytes(&self.state.borrow().buf)
}
}
impl<Find, Predicate, StateMut, E> Iterator for Ancestors<Find, Predicate, StateMut>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
Predicate: FnMut(&oid) -> bool,
StateMut: BorrowMut<State>,
E: std::error::Error + Send + Sync + 'static,
{
type Item = Result<ObjectId, Error>;
fn next(&mut self) -> Option<Self::Item> {
if matches!(self.parents, Parents::First) {
self.next_by_topology()
} else {
match self.sorting {
Sorting::Topological => self.next_by_topology(),
Sorting::ByCommitTimeNewestFirst => self.next_by_commit_date(None),
Sorting::ByCommitTimeNewestFirstCutoffOlderThan {
time_in_seconds_since_epoch,
} => self.next_by_commit_date(time_in_seconds_since_epoch.into()),
}
}
}
}
impl Sorting {
/// If not topo sort, provide the cutoff date if present.
fn cutoff_time(&self) -> Option<u32> {
match self {
Sorting::ByCommitTimeNewestFirstCutoffOlderThan {
time_in_seconds_since_epoch,
} => Some(*time_in_seconds_since_epoch),
_ => None,
}
}
}
/// Utilities
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
Predicate: FnMut(&oid) -> bool,
StateMut: BorrowMut<State>,
E: std::error::Error + Send + Sync + 'static,
{
fn next_by_commit_date(&mut self, cutoff_older_than: Option<TimeInSeconds>) -> Option<Result<ObjectId, Error>> {
let state = self.state.borrow_mut();
let (oid, _commit_time) = state.next.pop_front()?;
match (self.find)(&oid, &mut state.buf) {
Ok(commit_iter) => {
let mut count = 0;
for token in commit_iter {
count += 1;
let is_first = count == 1;
match token {
Ok(gix_object::commit::ref_iter::Token::Tree { .. }) => continue,
Ok(gix_object::commit::ref_iter::Token::Parent { id }) => {
let was_inserted = state.seen.insert(id);
if !(was_inserted && (self.predicate)(&id)) {
if is_first && matches!(self.parents, Parents::First) {
break;
} else {
continue;
}
}
let parent = (self.find)(id.as_ref(), &mut state.parents_buf).ok();
let parent_commit_time = parent
.and_then(|parent| {
parent
.committer()
.ok()
.map(|committer| committer.time.seconds_since_unix_epoch)
})
.unwrap_or_default();
let pos = match state.next.binary_search_by(|c| c.1.cmp(&parent_commit_time).reverse())
{
Ok(_) => None,
Err(pos) => Some(pos),
};
match cutoff_older_than {
Some(cutoff_older_than) if parent_commit_time < cutoff_older_than => continue,
Some(_) | None => match pos {
Some(pos) => state.next.insert(pos, (id, parent_commit_time)),
None => state.next.push_back((id, parent_commit_time)),
},
}
if is_first && matches!(self.parents, Parents::First) {
break;
}
}
Ok(_unused_token) => break,
Err(err) => return Some(Err(err.into())),
}
}
}
Err(err) => {
return Some(Err(Error::FindExisting {
oid,
source: err.into(),
}))
}
}
Some(Ok(oid))
}
}
/// Utilities
impl<Find, Predicate, StateMut, E> Ancestors<Find, Predicate, StateMut>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<CommitRefIter<'a>, E>,
Predicate: FnMut(&oid) -> bool,
StateMut: BorrowMut<State>,
E: std::error::Error + Send + Sync + 'static,
{
fn next_by_topology(&mut self) -> Option<Result<ObjectId, Error>> {
let state = self.state.borrow_mut();
let (oid, _commit_time) = state.next.pop_front()?;
match (self.find)(&oid, &mut state.buf) {
Ok(commit_iter) => {
for token in commit_iter {
match token {
Ok(gix_object::commit::ref_iter::Token::Tree { .. }) => continue,
Ok(gix_object::commit::ref_iter::Token::Parent { id }) => {
let was_inserted = state.seen.insert(id);
if was_inserted && (self.predicate)(&id) {
state.next.push_back((id, 0));
}
if matches!(self.parents, Parents::First) {
break;
}
}
Ok(_a_token_past_the_parents) => break,
Err(err) => return Some(Err(err.into())),
}
}
}
Err(err) => {
return Some(Err(Error::FindExisting {
oid,
source: err.into(),
}))
}
}
Some(Ok(oid))
}
}
}
|