radicle: add ReadRepository::contains

It can be useful to be able to check if a repository contains a given
commit. Add a `contains` method to the `ReadRepository` trait to
handle this.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2023-11-10 11:06:55 +00:00 committed by cloudhead
parent 40303cd8e1
commit ac849f1fd5
No known key found for this signature in database
4 changed files with 18 additions and 0 deletions

View File

@ -433,6 +433,9 @@ pub trait ReadRepository: Sized + ValidateRepository {
/// Perform a revision walk of a commit history starting from the given head.
fn revwalk(&self, head: Oid) -> Result<git2::Revwalk, git2::Error>;
/// Check if the underlying ODB contains the given `oid`.
fn contains(&self, oid: Oid) -> Result<bool, git2::Error>;
/// Check whether the given commit is an ancestor of another commit.
fn is_ancestor_of(&self, ancestor: Oid, head: Oid) -> Result<bool, git::ext::Error>;

View File

@ -565,6 +565,10 @@ impl ReadRepository for Repository {
Ok(revwalk)
}
fn contains(&self, oid: Oid) -> Result<bool, raw::Error> {
self.backend.odb().map(|odb| odb.exists(oid.into()))
}
fn is_ancestor_of(&self, ancestor: Oid, head: Oid) -> Result<bool, git::Error> {
self.backend
.graph_descendant_of(head.into(), ancestor.into())

View File

@ -278,6 +278,10 @@ impl<'a, R: storage::ReadRepository> ReadRepository for DraftStore<'a, R> {
self.repo.revwalk(head)
}
fn contains(&self, oid: Oid) -> Result<bool, raw::Error> {
self.repo.contains(oid)
}
fn is_ancestor_of(&self, ancestor: Oid, head: Oid) -> Result<bool, git_ext::Error> {
self.repo.is_ancestor_of(ancestor, head)
}

View File

@ -188,6 +188,13 @@ impl ReadRepository for MockRepository {
todo!()
}
fn contains(&self, oid: Oid) -> Result<bool, git2::Error> {
Ok(self
.remotes
.values()
.any(|sigrefs| sigrefs.at == oid || sigrefs.refs.values().any(|oid_| *oid_ == oid)))
}
fn is_ancestor_of(&self, _ancestor: Oid, _head: Oid) -> Result<bool, git_ext::Error> {
Ok(true)
}