radicle: check if ref exists when removing a cob

If the reference did not exist it is not necessary to create a new
`sigrefs` entry. Check that the reference exists before removing from
the cob store and only sign if it did.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2023-04-27 15:21:43 +01:00 committed by Alexis Sellier
parent 0e6fae5a03
commit 44a3a09e5c
No known key found for this signature in database
1 changed files with 23 additions and 4 deletions

View File

@ -105,6 +105,12 @@ pub enum Error {
NotFound(TypeName, ObjectId),
#[error("signed refs: {0}")]
SignRefs(#[from] storage::Error),
#[error("failed to find reference '{name}': {err}")]
RefLookup {
name: git::RefString,
#[source]
err: git::Error,
},
}
/// Storage for collaborative objects of a specific type `T` in a single repository.
@ -237,10 +243,23 @@ where
/// Remove an object.
pub fn remove<G: Signer>(&self, id: &ObjectId, signer: &G) -> Result<(), Error> {
cob::remove(self.repo, signer.public_key(), T::type_name(), id)?;
self.repo.sign_refs(signer).map_err(Error::SignRefs)?;
Ok(())
let name = git::refs::storage::cob(signer.public_key(), T::type_name(), id);
match self
.repo
.reference_oid(signer.public_key(), &name.strip_namespace())
{
Ok(_) => {
cob::remove(self.repo, signer.public_key(), T::type_name(), id)?;
self.repo.sign_refs(signer).map_err(Error::SignRefs)?;
Ok(())
}
Err(git::Error::NotFound(_)) => Ok(()),
Err(git::Error::Git(err)) if err.code() == git::raw::ErrorCode::NotFound => Ok(()),
Err(err) => Err(Error::RefLookup {
name: name.to_ref_string(),
err,
}),
}
}
}