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
use git_hash::ObjectId;
use git_odb::FindExt;
use crate::{revision, Repository};
pub struct Platform<'repo> {
pub(crate) repo: &'repo Repository,
pub(crate) tips: Vec<ObjectId>,
pub(crate) sorting: git_traverse::commit::Sorting,
pub(crate) parents: git_traverse::commit::Parents,
}
impl<'repo> Platform<'repo> {
pub(crate) fn new(tips: impl IntoIterator<Item = impl Into<ObjectId>>, repo: &'repo Repository) -> Self {
revision::walk::Platform {
repo,
tips: tips.into_iter().map(Into::into).collect(),
sorting: Default::default(),
parents: Default::default(),
}
}
}
impl<'repo> Platform<'repo> {
pub fn sorting(mut self, sorting: git_traverse::commit::Sorting) -> Self {
self.sorting = sorting;
self
}
pub fn first_parent_only(mut self) -> Self {
self.parents = git_traverse::commit::Parents::First;
self
}
}
impl<'repo> Platform<'repo> {
pub fn all(self) -> Result<revision::Walk<'repo>, git_traverse::commit::ancestors::Error> {
let Platform {
repo,
tips,
sorting,
parents,
} = self;
Ok(revision::Walk {
repo,
inner: Box::new(
git_traverse::commit::Ancestors::new(
tips,
git_traverse::commit::ancestors::State::default(),
move |oid, buf| repo.objects.find_commit_iter(oid, buf),
)
.sorting(sorting)?
.parents(parents),
),
is_shallow: None,
error_on_missing_commit: false,
})
}
}
pub(crate) mod iter {
use crate::{ext::ObjectIdExt, Id};
pub struct Walk<'repo> {
pub(crate) repo: &'repo crate::Repository,
pub(crate) inner:
Box<dyn Iterator<Item = Result<git_hash::ObjectId, git_traverse::commit::ancestors::Error>> + 'repo>,
pub(crate) error_on_missing_commit: bool,
pub is_shallow: Option<bool>,
}
impl<'repo> Walk<'repo> {
pub fn error_on_missing_commit(mut self) -> Self {
self.error_on_missing_commit = true;
self
}
}
impl<'repo> Iterator for Walk<'repo> {
type Item = Result<Id<'repo>, git_traverse::commit::ancestors::Error>;
fn next(&mut self) -> Option<Self::Item> {
match self.inner.next() {
None => {
self.is_shallow = Some(false);
None
}
Some(Ok(oid)) => Some(Ok(oid.attach(self.repo))),
Some(Err(err @ git_traverse::commit::ancestors::Error::FindExisting { .. })) => {
if self.error_on_missing_commit {
Some(Err(err))
} else {
self.is_shallow = Some(true);
None
}
}
Some(Err(err)) => Some(Err(err)),
}
}
}
}