radicle_git_ext/
revwalk.rs

1pub enum Start {
2    Oid(git2::Oid),
3    Ref(String),
4}
5
6pub struct FirstParent<'a> {
7    inner: git2::Revwalk<'a>,
8}
9
10impl<'a> FirstParent<'a> {
11    pub fn new(repo: &'a git2::Repository, start: Start) -> Result<Self, git2::Error> {
12        let mut revwalk = repo.revwalk()?;
13        revwalk.set_sorting(git2::Sort::TOPOLOGICAL)?;
14        revwalk.simplify_first_parent()?;
15
16        match start {
17            Start::Oid(oid) => revwalk.push(oid),
18            Start::Ref(name) => revwalk.push_ref(&name),
19        }?;
20
21        Ok(Self { inner: revwalk })
22    }
23
24    pub fn reverse(mut self) -> Result<Self, git2::Error> {
25        let mut sort = git2::Sort::TOPOLOGICAL;
26        sort.insert(git2::Sort::REVERSE);
27        self.inner.set_sorting(sort)?;
28        Ok(self)
29    }
30}
31
32impl<'a> IntoIterator for FirstParent<'a> {
33    type Item = Result<git2::Oid, git2::Error>;
34    type IntoIter = git2::Revwalk<'a>;
35
36    fn into_iter(self) -> Self::IntoIter {
37        self.inner
38    }
39}