Skip to content

Commit 839dd0f

Browse files
committed
add tests for new simple commit ordering
1 parent 1dbb94a commit 839dd0f

File tree

2 files changed

+183
-88
lines changed

2 files changed

+183
-88
lines changed

gix-traverse/src/commit/simple.rs

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use std::cmp::Reverse;
66
use std::collections::VecDeque;
77

88
#[derive(Default, Debug, Copy, Clone)]
9-
/// The order with which to prioritize the search
9+
/// The order with which to prioritize the search.
1010
pub enum CommitTimeOrder {
1111
#[default]
12-
/// sort commits by newest first
12+
/// Sort commits by newest first.
1313
NewestFirst,
14-
/// sort commits by oldest first
14+
/// Sort commits by oldest first.
1515
OldestFirst,
1616
}
1717

@@ -31,7 +31,7 @@ pub enum CommitTimeOrder {
3131
pub enum Sorting {
3232
/// Commits are sorted as they are mentioned in the commit graph.
3333
///
34-
/// In the *sample history* the order would be `8, 6, 7, 5, 4, 3, 2, 1`
34+
/// In the *sample history* the order would be `8, 6, 7, 5, 4, 3, 2, 1`.
3535
///
3636
/// ### Note
3737
///
@@ -43,22 +43,22 @@ pub enum Sorting {
4343
///
4444
/// The sorting applies to all currently queued commit ids and thus is full.
4545
///
46-
/// In the *sample history* the order would be `8, 7, 6, 5, 4, 3, 2, 1` for NewestFirst
47-
/// Or `1, 2, 3, 4, 5, 6, 7, 8` for OldestFirst
46+
/// In the *sample history* the order would be `8, 7, 6, 5, 4, 3, 2, 1` for [`NewestFirst`](CommitTimeOrder::NewestFirst),
47+
/// or `1, 2, 3, 4, 5, 6, 7, 8` for [`OldestFirst`](CommitTimeOrder::OldestFirst).
4848
///
4949
/// # Performance
5050
///
5151
/// This mode benefits greatly from having an object_cache in `find()`
5252
/// to avoid having to lookup each commit twice.
5353
ByCommitTime(CommitTimeOrder),
54-
/// This sorting is similar to `ByCommitTime`, but adds a cutoff to not return commits older than
54+
/// This sorting is similar to [`ByCommitTime`](Sorting::ByCommitTime), but adds a cutoff to not return commits older than
5555
/// a given time, stopping the iteration once no younger commits is queued to be traversed.
5656
///
5757
/// As the query is usually repeated with different cutoff dates, this search mode benefits greatly from an object cache.
5858
///
59-
/// In the *sample history* and a cut-off date of 4, the returned list of commits would be `8, 7, 6, 4`
59+
/// In the *sample history* and a cut-off date of 4, the returned list of commits would be `8, 7, 6, 4`.
6060
ByCommitTimeCutoff {
61-
/// The order in wich to prioritize lookups
61+
/// The order in which to prioritize lookups.
6262
order: CommitTimeOrder,
6363
/// The amount of seconds since unix epoch, the same value obtained by any `gix_date::Time` structure and the way git counts time.
6464
seconds: gix_date::SecondsSinceUnixEpoch,
@@ -125,11 +125,10 @@ mod init {
125125
}
126126
}
127127

128-
fn order_time(i: i64, order: CommitTimeOrder) -> super::QueueKey<i64> {
129-
if let CommitTimeOrder::NewestFirst = order {
130-
Newest(i)
131-
} else {
132-
Oldest(Reverse(i))
128+
fn to_queue_key(i: i64, order: CommitTimeOrder) -> super::QueueKey<i64> {
129+
match order {
130+
CommitTimeOrder::NewestFirst => Newest(i),
131+
CommitTimeOrder::OldestFirst => Oldest(Reverse(i)),
133132
}
134133
}
135134

@@ -151,17 +150,14 @@ mod init {
151150
for commit_id in state.next.drain(..) {
152151
let commit_iter = self.objects.find_commit_iter(&commit_id, &mut state.buf)?;
153152
let time = commit_iter.committer()?.time.seconds;
154-
let ordered_time = order_time(time, order);
153+
let key = to_queue_key(time, order);
155154
match (cutoff_time, order) {
156-
(Some(cutoff_time), CommitTimeOrder::NewestFirst) if time >= cutoff_time => {
157-
state.queue.insert(ordered_time, commit_id);
158-
}
159-
(Some(cutoff_time), CommitTimeOrder::OldestFirst) if time <= cutoff_time => {
160-
state.queue.insert(ordered_time, commit_id);
155+
(Some(cutoff_time), _) if time >= cutoff_time => {
156+
state.queue.insert(key, commit_id);
161157
}
162158
(Some(_), _) => {}
163159
(None, _) => {
164-
state.queue.insert(ordered_time, commit_id);
160+
state.queue.insert(key, commit_id);
165161
}
166162
}
167163
}
@@ -334,19 +330,10 @@ mod init {
334330
continue;
335331
}
336332

337-
let time = order_time(parent_commit_time, order);
338-
match (cutoff, order) {
339-
(Some(cutoff_older_than), CommitTimeOrder::NewestFirst)
340-
if parent_commit_time < cutoff_older_than =>
341-
{
342-
continue
343-
}
344-
(Some(cutoff_newer_than), CommitTimeOrder::OldestFirst)
345-
if parent_commit_time > cutoff_newer_than =>
346-
{
347-
continue
348-
}
349-
(Some(_) | None, _) => state.queue.insert(time, id),
333+
let key = to_queue_key(parent_commit_time, order);
334+
match cutoff {
335+
Some(cutoff_older_than) if parent_commit_time < cutoff_older_than => continue,
336+
Some(_) | None => state.queue.insert(key, id),
350337
}
351338
}
352339
}
@@ -366,19 +353,10 @@ mod init {
366353
.and_then(|parent| parent.committer().ok().map(|committer| committer.time.seconds))
367354
.unwrap_or_default();
368355

369-
let time = order_time(parent_commit_time, order);
370-
match (cutoff, order) {
371-
(Some(cutoff_older_than), CommitTimeOrder::NewestFirst)
372-
if parent_commit_time < cutoff_older_than =>
373-
{
374-
continue
375-
}
376-
(Some(cutoff_newer_than), CommitTimeOrder::OldestFirst)
377-
if parent_commit_time > cutoff_newer_than =>
378-
{
379-
continue
380-
}
381-
(Some(_) | None, _) => state.queue.insert(time, id),
356+
let time = to_queue_key(parent_commit_time, order);
357+
match cutoff {
358+
Some(cutoff_older_than) if parent_commit_time < cutoff_older_than => continue,
359+
Some(_) | None => state.queue.insert(time, id),
382360
}
383361
}
384362
Ok(_unused_token) => break,

0 commit comments

Comments
 (0)