Move `sign_refs` to `WriteRepository` trait
Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
657a951df0
commit
94f7675c72
|
|
@ -107,7 +107,7 @@ pub fn run(profile: radicle::Profile) -> Result<(), Box<dyn std::error::Error +
|
||||||
|
|
||||||
if child.wait()?.success() {
|
if child.wait()?.success() {
|
||||||
if *service == GIT_RECEIVE_PACK {
|
if *service == GIT_RECEIVE_PACK {
|
||||||
profile.storage.sign_refs(&proj, &profile.signer)?;
|
proj.sign_refs(&profile.signer)?;
|
||||||
proj.set_head()?;
|
proj.set_head()?;
|
||||||
// Connect to local node and announce refs to the network.
|
// Connect to local node and announce refs to the network.
|
||||||
// If our node is not running, we simply skip this step, as the
|
// If our node is not running, we simply skip this step, as the
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use radicle::{node::Handle, storage::WriteStorage};
|
use radicle::{node::Handle, storage::WriteRepository, storage::WriteStorage};
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let cwd = Path::new(".").canonicalize()?;
|
let cwd = Path::new(".").canonicalize()?;
|
||||||
|
|
@ -12,9 +12,12 @@ fn main() -> anyhow::Result<()> {
|
||||||
println!("{}", output);
|
println!("{}", output);
|
||||||
|
|
||||||
let project = profile.storage.repository(id)?;
|
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)?;
|
profile.node()?.announce_refs(&id)?;
|
||||||
|
|
||||||
|
println!("head: {}", head);
|
||||||
println!("ok: {}", sigrefs.signature);
|
println!("ok: {}", sigrefs.signature);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ pub fn init<G: Signer, S: storage::WriteStorage>(
|
||||||
|
|
||||||
git::configure_remote(repo, &REMOTE_NAME, &url)?;
|
git::configure_remote(repo, &REMOTE_NAME, &url)?;
|
||||||
git::push(repo, &REMOTE_NAME, pk, [(&default_branch, &default_branch)])?;
|
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()?;
|
let _head = project.set_head()?;
|
||||||
|
|
||||||
Ok((id, signed))
|
Ok((id, signed))
|
||||||
|
|
@ -147,7 +147,7 @@ pub fn fork_remote<G: Signer, S: storage::WriteStorage>(
|
||||||
&format!("creating identity branch for {me}"),
|
&format!("creating identity branch for {me}"),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
storage.sign_refs(&repository, &signer)?;
|
repository.sign_refs(&signer)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -178,7 +178,7 @@ pub fn fork<G: Signer, S: storage::WriteStorage>(
|
||||||
false,
|
false,
|
||||||
&format!("creating identity branch for {me}"),
|
&format!("creating identity branch for {me}"),
|
||||||
)?;
|
)?;
|
||||||
storage.sign_refs(&repository, &signer)?;
|
repository.sign_refs(&signer)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,8 +41,6 @@ pub enum Error {
|
||||||
Id(#[from] IdError),
|
Id(#[from] IdError),
|
||||||
#[error("i/o: {0}")]
|
#[error("i/o: {0}")]
|
||||||
Io(#[from] io::Error),
|
Io(#[from] io::Error),
|
||||||
#[error("invalid repository head")]
|
|
||||||
InvalidHead,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch error.
|
/// Fetch error.
|
||||||
|
|
@ -232,12 +230,6 @@ pub trait WriteStorage: ReadStorage {
|
||||||
type Repository: WriteRepository;
|
type Repository: WriteRepository;
|
||||||
|
|
||||||
fn repository(&self, proj: Id) -> Result<Self::Repository, Error>;
|
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>;
|
fn fetch(&self, proj_id: Id, remote: &Url) -> Result<Vec<RefUpdate>, FetchError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -286,6 +278,7 @@ pub trait ReadRepository {
|
||||||
pub trait WriteRepository: ReadRepository {
|
pub trait WriteRepository: ReadRepository {
|
||||||
fn fetch(&mut self, url: &Url) -> Result<Vec<RefUpdate>, FetchError>;
|
fn fetch(&mut self, url: &Url) -> Result<Vec<RefUpdate>, FetchError>;
|
||||||
fn set_head(&self) -> Result<Oid, ProjectError>;
|
fn set_head(&self) -> Result<Oid, ProjectError>;
|
||||||
|
fn sign_refs<G: Signer>(&self, signer: G) -> Result<SignedRefs<Verified>, Error>;
|
||||||
fn raw(&self) -> &git2::Repository;
|
fn raw(&self) -> &git2::Repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -326,14 +319,6 @@ where
|
||||||
self.deref().repository(proj)
|
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> {
|
fn fetch(&self, proj_id: Id, remote: &Url) -> Result<Vec<RefUpdate>, FetchError> {
|
||||||
self.deref().fetch(proj_id, remote)
|
self.deref().fetch(proj_id, remote)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -108,14 +108,6 @@ impl WriteStorage for Storage {
|
||||||
Repository::open(paths::repository(self, &proj), proj)
|
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> {
|
fn fetch(&self, proj_id: Id, remote: &Url) -> Result<Vec<RefUpdate>, FetchError> {
|
||||||
let mut repo = self.repository(proj_id)?;
|
let mut repo = self.repository(proj_id)?;
|
||||||
let mut path = remote.path.clone();
|
let mut path = remote.path.clone();
|
||||||
|
|
@ -399,16 +391,6 @@ impl Repository {
|
||||||
);
|
);
|
||||||
Ok(remotes)
|
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 {
|
impl ReadRepository for Repository {
|
||||||
|
|
@ -660,6 +642,16 @@ impl WriteRepository for Repository {
|
||||||
Ok(head)
|
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 {
|
fn raw(&self) -> &git2::Repository {
|
||||||
&self.backend
|
&self.backend
|
||||||
}
|
}
|
||||||
|
|
@ -845,7 +837,7 @@ mod tests {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.id();
|
.id();
|
||||||
git::push(&proj_repo, "rad", alice_id, [(&refname, &refname)]).unwrap();
|
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();
|
alice_proj_storage.set_head().unwrap();
|
||||||
|
|
||||||
// Have Bob fetch Alice's new commit.
|
// Have Bob fetch Alice's new commit.
|
||||||
|
|
@ -990,7 +982,7 @@ mod tests {
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let signed = storage.sign_refs(&project, &signer).unwrap();
|
let signed = project.sign_refs(&signer).unwrap();
|
||||||
let remote = project.remote(&alice).unwrap();
|
let remote = project.remote(&alice).unwrap();
|
||||||
let mut unsigned = project.references(&alice).unwrap();
|
let mut unsigned = project.references(&alice).unwrap();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,14 +66,6 @@ impl WriteStorage for MockStorage {
|
||||||
Ok(MockRepository {})
|
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> {
|
fn fetch(&self, _proj_id: Id, _remote: &Url) -> Result<Vec<RefUpdate>, FetchError> {
|
||||||
Ok(vec![])
|
Ok(vec![])
|
||||||
}
|
}
|
||||||
|
|
@ -161,4 +153,11 @@ impl WriteRepository for MockRepository {
|
||||||
fn set_head(&self) -> Result<Oid, ProjectError> {
|
fn set_head(&self) -> Result<Oid, ProjectError> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn sign_refs<G: Signer>(
|
||||||
|
&self,
|
||||||
|
_signer: G,
|
||||||
|
) -> Result<crate::storage::refs::SignedRefs<Verified>, Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue