Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle: ensure clean up on rad::init error
Fintan Halpenny committed 2 years ago
commit 5dbd9bb2a32dca6aafbd2b4a50dd73d237ca025d
parent f03c734e85c51e1b5baf51dfe6006eacef261527
3 files changed +34 -10
modified radicle/src/rad.rs
@@ -70,17 +70,44 @@ pub fn init<G: Signer, S: WriteStorage>(
        )
    })?;
    let doc = identity::Doc::initial(proj, delegate, visibility).verified()?;
-
    let (project, _) = Repository::init(&doc, storage, signer)?;
+
    let (project, _) = Repository::init(&doc, &storage, signer)?;
    let url = git::Url::from(project.id);

+
    match init_configure(repo, &project, pk, &default_branch, &url, signer) {
+
        Ok(signed) => Ok((project.id, doc, signed)),
+
        Err(err) => {
+
            if let Err(e) = project.remove() {
+
                log::warn!(target: "radicle", "Failed to remove project during `rad::init` cleanup: {e}");
+
            }
+
            if repo.find_remote(&REMOTE_NAME).is_ok() {
+
                if let Err(e) = repo.remote_delete(&REMOTE_NAME) {
+
                    log::warn!(target: "radicle", "Failed to remove remote during `rad::init` cleanup: {e}");
+
                }
+
            }
+
            Err(err)
+
        }
+
    }
+
}
+

+
fn init_configure<G>(
+
    repo: &git2::Repository,
+
    project: &Repository,
+
    pk: &crypto::PublicKey,
+
    default_branch: &BranchName,
+
    url: &git::Url,
+
    signer: &G,
+
) -> Result<SignedRefs<Verified>, InitError>
+
where
+
    G: crypto::Signer,
+
{
    git::configure_repository(repo)?;
-
    git::configure_remote(repo, &REMOTE_NAME, &url, &url.clone().with_namespace(*pk))?;
+
    git::configure_remote(repo, &REMOTE_NAME, url, &url.clone().with_namespace(*pk))?;
    git::push(
        repo,
        &REMOTE_NAME,
        [(
-
            &git::fmt::lit::refs_heads(&default_branch).into(),
-
            &git::fmt::lit::refs_heads(&default_branch).into(),
+
            &git::fmt::lit::refs_heads(default_branch).into(),
+
            &git::fmt::lit::refs_heads(default_branch).into(),
        )],
    )?;

@@ -88,7 +115,7 @@ pub fn init<G: Signer, S: WriteStorage>(
    let _head = project.set_identity_head()?;
    let _head = project.set_head()?;

-
    Ok((project.id, doc, signed))
+
    Ok(signed)
}

#[derive(Error, Debug)]
modified radicle/src/storage.rs
@@ -371,9 +371,6 @@ pub trait WriteStorage: ReadStorage {
    /// Create a read-write repository.
    fn create(&self, rid: Id) -> Result<Self::RepositoryMut, Error>;

-
    /// Delete all remote namespaces apart from the local node's and
-
    /// delegates' namespace.
-

    /// Clean the repository found at `rid`.
    ///
    /// If the local peer has initialised `rad/sigrefs` by forking or
modified radicle/src/storage/git.rs
@@ -422,12 +422,12 @@ impl Repository {
    /// Create the repository's identity branch.
    pub fn init<G: Signer, S: WriteStorage>(
        doc: &Doc<Verified>,
-
        storage: S,
+
        storage: &S,
        signer: &G,
    ) -> Result<(Self, git::Oid), RepositoryError> {
        let (doc_oid, _) = doc.encode()?;
        let id = Id::from(doc_oid);
-
        let repo = Self::create(paths::repository(&storage, &id), id, storage.info())?;
+
        let repo = Self::create(paths::repository(storage, &id), id, storage.info())?;
        let commit = doc.init(&repo, signer)?;

        Ok((repo, commit))