Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
Move `sign_refs` to `WriteRepository` trait
Alexis Sellier committed 3 years ago
commit 94f7675c72867de3bf97e4d5c42154f9fba87770
parent 657a951df0c6b4de6417176e8295fc14fbe68742
6 files changed +29 -50
modified radicle-remote-helper/src/lib.rs
@@ -107,7 +107,7 @@ pub fn run(profile: radicle::Profile) -> Result<(), Box<dyn std::error::Error +

                if child.wait()?.success() {
                    if *service == GIT_RECEIVE_PACK {
-
                        profile.storage.sign_refs(&proj, &profile.signer)?;
+
                        proj.sign_refs(&profile.signer)?;
                        proj.set_head()?;
                        // Connect to local node and announce refs to the network.
                        // If our node is not running, we simply skip this step, as the
modified radicle-tools/src/rad-push.rs
@@ -1,6 +1,6 @@
use std::path::Path;

-
use radicle::{node::Handle, storage::WriteStorage};
+
use radicle::{node::Handle, storage::WriteRepository, storage::WriteStorage};

fn main() -> anyhow::Result<()> {
    let cwd = Path::new(".").canonicalize()?;
@@ -12,9 +12,12 @@ fn main() -> anyhow::Result<()> {
    println!("{}", output);

    let project = profile.storage.repository(id)?;
-
    let sigrefs = profile.storage.sign_refs(&project, &profile.signer)?;
+
    let sigrefs = project.sign_refs(&profile.signer)?;
+
    let head = project.set_head()?;
+

    profile.node()?.announce_refs(&id)?;

+
    println!("head: {}", head);
    println!("ok: {}", sigrefs.signature);

    Ok(())
modified radicle/src/rad.rs
@@ -76,7 +76,7 @@ pub fn init<G: Signer, S: storage::WriteStorage>(

    git::configure_remote(repo, &REMOTE_NAME, &url)?;
    git::push(repo, &REMOTE_NAME, pk, [(&default_branch, &default_branch)])?;
-
    let signed = storage.sign_refs(&project, signer)?;
+
    let signed = project.sign_refs(signer)?;
    let _head = project.set_head()?;

    Ok((id, signed))
@@ -147,7 +147,7 @@ pub fn fork_remote<G: Signer, S: storage::WriteStorage>(
        &format!("creating identity branch for {me}"),
    )?;

-
    storage.sign_refs(&repository, &signer)?;
+
    repository.sign_refs(&signer)?;

    Ok(())
}
@@ -178,7 +178,7 @@ pub fn fork<G: Signer, S: storage::WriteStorage>(
        false,
        &format!("creating identity branch for {me}"),
    )?;
-
    storage.sign_refs(&repository, &signer)?;
+
    repository.sign_refs(&signer)?;

    Ok(())
}
modified radicle/src/storage.rs
@@ -41,8 +41,6 @@ pub enum Error {
    Id(#[from] IdError),
    #[error("i/o: {0}")]
    Io(#[from] io::Error),
-
    #[error("invalid repository head")]
-
    InvalidHead,
}

/// Fetch error.
@@ -232,12 +230,6 @@ pub trait WriteStorage: ReadStorage {
    type Repository: WriteRepository;

    fn repository(&self, proj: Id) -> Result<Self::Repository, Error>;
-
    // TODO: Move this to `WriteRepository`.
-
    fn sign_refs<G: Signer>(
-
        &self,
-
        repository: &Self::Repository,
-
        signer: G,
-
    ) -> Result<SignedRefs<Verified>, Error>;
    fn fetch(&self, proj_id: Id, remote: &Url) -> Result<Vec<RefUpdate>, FetchError>;
}

@@ -286,6 +278,7 @@ pub trait ReadRepository {
pub trait WriteRepository: ReadRepository {
    fn fetch(&mut self, url: &Url) -> Result<Vec<RefUpdate>, FetchError>;
    fn set_head(&self) -> Result<Oid, ProjectError>;
+
    fn sign_refs<G: Signer>(&self, signer: G) -> Result<SignedRefs<Verified>, Error>;
    fn raw(&self) -> &git2::Repository;
}

@@ -326,14 +319,6 @@ where
        self.deref().repository(proj)
    }

-
    fn sign_refs<G: Signer>(
-
        &self,
-
        repository: &S::Repository,
-
        signer: G,
-
    ) -> Result<SignedRefs<Verified>, Error> {
-
        self.deref().sign_refs(repository, signer)
-
    }
-

    fn fetch(&self, proj_id: Id, remote: &Url) -> Result<Vec<RefUpdate>, FetchError> {
        self.deref().fetch(proj_id, remote)
    }
modified radicle/src/storage/git.rs
@@ -108,14 +108,6 @@ impl WriteStorage for Storage {
        Repository::open(paths::repository(self, &proj), proj)
    }

-
    fn sign_refs<G: Signer>(
-
        &self,
-
        repository: &Repository,
-
        signer: G,
-
    ) -> Result<SignedRefs<Verified>, Error> {
-
        repository.sign_refs(signer)
-
    }
-

    fn fetch(&self, proj_id: Id, remote: &Url) -> Result<Vec<RefUpdate>, FetchError> {
        let mut repo = self.repository(proj_id)?;
        let mut path = remote.path.clone();
@@ -399,16 +391,6 @@ impl Repository {
        );
        Ok(remotes)
    }
-

-
    pub fn sign_refs<G: Signer>(&self, signer: G) -> Result<SignedRefs<Verified>, Error> {
-
        let remote = signer.public_key();
-
        let refs = self.references(remote)?;
-
        let signed = refs.signed(&signer)?;
-

-
        signed.save(remote, self)?;
-

-
        Ok(signed)
-
    }
}

impl ReadRepository for Repository {
@@ -660,6 +642,16 @@ impl WriteRepository for Repository {
        Ok(head)
    }

+
    fn sign_refs<G: Signer>(&self, signer: G) -> Result<SignedRefs<Verified>, Error> {
+
        let remote = signer.public_key();
+
        let refs = self.references(remote)?;
+
        let signed = refs.signed(&signer)?;
+

+
        signed.save(remote, self)?;
+

+
        Ok(signed)
+
    }
+

    fn raw(&self) -> &git2::Repository {
        &self.backend
    }
@@ -845,7 +837,7 @@ mod tests {
            .unwrap()
            .id();
        git::push(&proj_repo, "rad", alice_id, [(&refname, &refname)]).unwrap();
-
        alice.sign_refs(&alice_proj_storage, &alice_signer).unwrap();
+
        alice_proj_storage.sign_refs(&alice_signer).unwrap();
        alice_proj_storage.set_head().unwrap();

        // Have Bob fetch Alice's new commit.
@@ -990,7 +982,7 @@ mod tests {
        )
        .unwrap();

-
        let signed = storage.sign_refs(&project, &signer).unwrap();
+
        let signed = project.sign_refs(&signer).unwrap();
        let remote = project.remote(&alice).unwrap();
        let mut unsigned = project.references(&alice).unwrap();

modified radicle/src/test/storage.rs
@@ -66,14 +66,6 @@ impl WriteStorage for MockStorage {
        Ok(MockRepository {})
    }

-
    fn sign_refs<G: Signer>(
-
        &self,
-
        _repository: &Self::Repository,
-
        _signer: G,
-
    ) -> Result<crate::storage::refs::SignedRefs<Verified>, Error> {
-
        todo!()
-
    }
-

    fn fetch(&self, _proj_id: Id, _remote: &Url) -> Result<Vec<RefUpdate>, FetchError> {
        Ok(vec![])
    }
@@ -161,4 +153,11 @@ impl WriteRepository for MockRepository {
    fn set_head(&self) -> Result<Oid, ProjectError> {
        todo!()
    }
+

+
    fn sign_refs<G: Signer>(
+
        &self,
+
        _signer: G,
+
    ) -> Result<crate::storage::refs::SignedRefs<Verified>, Error> {
+
        todo!()
+
    }
}