diff --git a/radicle/src/storage.rs b/radicle/src/storage.rs index 002b0f9e..b21fdab5 100644 --- a/radicle/src/storage.rs +++ b/radicle/src/storage.rs @@ -254,6 +254,8 @@ pub trait ReadStorage { remote: &RemoteId, rid: Id, ) -> Result>, ProjectError>; + /// Check whether storage contains a repository. + fn contains(&self, rid: &Id) -> Result; /// Get the inventory of repositories hosted under this storage. fn inventory(&self) -> Result; } @@ -357,6 +359,10 @@ where self.deref().path() } + fn contains(&self, rid: &Id) -> Result { + self.deref().contains(rid) + } + fn inventory(&self) -> Result { self.deref().inventory() } diff --git a/radicle/src/storage/git.rs b/radicle/src/storage/git.rs index c5a32f2b..862ebacd 100644 --- a/radicle/src/storage/git.rs +++ b/radicle/src/storage/git.rs @@ -99,6 +99,14 @@ impl ReadStorage for Storage { self.path.as_path() } + fn contains(&self, rid: &Id) -> Result { + if paths::repository(&self, rid).exists() { + let _ = self.repository(*rid)?.head()?; + return Ok(true); + } + Ok(false) + } + fn get(&self, remote: &RemoteId, proj: Id) -> Result>, ProjectError> { // TODO: Don't create a repo here if it doesn't exist? // Perhaps for checking we could have a `contains` method? diff --git a/radicle/src/test/storage.rs b/radicle/src/test/storage.rs index 32993627..f212631e 100644 --- a/radicle/src/test/storage.rs +++ b/radicle/src/test/storage.rs @@ -36,6 +36,10 @@ impl ReadStorage for MockStorage { self.path.as_path() } + fn contains(&self, rid: &Id) -> Result { + Ok(self.inventory.contains_key(rid)) + } + fn get( &self, _remote: &RemoteId,