radicle_git_ext/
revwalk.rs

1// Copyright © 2019-2020 The Radicle Foundation <hello@radicle.foundation>
2//
3// This file is part of radicle-link, distributed under the GPLv3 with Radicle
4// Linking Exception. For full terms see the included LICENSE file.
5
6pub enum Start {
7    Oid(git2::Oid),
8    Ref(String),
9}
10
11pub struct FirstParent<'a> {
12    inner: git2::Revwalk<'a>,
13}
14
15impl<'a> FirstParent<'a> {
16    pub fn new(repo: &'a git2::Repository, start: Start) -> Result<Self, git2::Error> {
17        let mut revwalk = repo.revwalk()?;
18        revwalk.set_sorting(git2::Sort::TOPOLOGICAL)?;
19        revwalk.simplify_first_parent()?;
20
21        match start {
22            Start::Oid(oid) => revwalk.push(oid),
23            Start::Ref(name) => revwalk.push_ref(&name),
24        }?;
25
26        Ok(Self { inner: revwalk })
27    }
28
29    pub fn reverse(mut self) -> Result<Self, git2::Error> {
30        let mut sort = git2::Sort::TOPOLOGICAL;
31        sort.insert(git2::Sort::REVERSE);
32        self.inner.set_sorting(sort)?;
33        Ok(self)
34    }
35}
36
37impl<'a> IntoIterator for FirstParent<'a> {
38    type Item = Result<git2::Oid, git2::Error>;
39    type IntoIter = git2::Revwalk<'a>;
40
41    fn into_iter(self) -> Self::IntoIter {
42        self.inner
43    }
44}