mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-29 05:50:31 +08:00
node: Use stronger types for refs
Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
+12
-8
@@ -214,7 +214,7 @@ impl ReadRepository for Repository {
|
||||
fn reference(
|
||||
&self,
|
||||
remote: &RemoteId,
|
||||
name: &str,
|
||||
name: &git::RefStr,
|
||||
) -> Result<Option<git2::Reference>, git2::Error> {
|
||||
let name = format!("refs/remotes/{remote}/{name}");
|
||||
self.backend.find_reference(&name).map(Some).or_else(|e| {
|
||||
@@ -226,7 +226,11 @@ impl ReadRepository for Repository {
|
||||
})
|
||||
}
|
||||
|
||||
fn reference_oid(&self, user: &RemoteId, reference: &str) -> Result<Option<Oid>, git2::Error> {
|
||||
fn reference_oid(
|
||||
&self,
|
||||
user: &RemoteId,
|
||||
reference: &git::RefStr,
|
||||
) -> Result<Option<Oid>, git2::Error> {
|
||||
let reference = self.reference(user, reference)?;
|
||||
Ok(reference.and_then(|r| r.target().map(|o| o.into())))
|
||||
}
|
||||
@@ -249,7 +253,7 @@ impl ReadRepository for Repository {
|
||||
let (_, refname) = git::parse_ref::<RemoteId>(name)?;
|
||||
let oid = e.target().ok_or(Error::InvalidRef)?;
|
||||
|
||||
refs.insert(refname.to_string(), oid.into());
|
||||
refs.insert(refname, oid.into());
|
||||
}
|
||||
Ok(refs.into())
|
||||
}
|
||||
@@ -338,7 +342,7 @@ mod tests {
|
||||
|
||||
// Strip the remote refs of sigrefs so we can compare them.
|
||||
for remote in refs.values_mut() {
|
||||
remote.remove(SIGNATURE_REF).unwrap();
|
||||
remote.remove(&*SIGNATURE_REF).unwrap();
|
||||
}
|
||||
assert_eq!(refs, remotes.into());
|
||||
}
|
||||
@@ -351,7 +355,7 @@ mod tests {
|
||||
let inventory = alice.inventory().unwrap();
|
||||
let proj = inventory.first().unwrap();
|
||||
let remotes = alice.repository(proj).unwrap().remotes().unwrap();
|
||||
let refname = "heads/master";
|
||||
let refname = git::refname!("heads/master");
|
||||
|
||||
// Have Bob fetch Alice's refs.
|
||||
bob.repository(proj)
|
||||
@@ -366,10 +370,10 @@ mod tests {
|
||||
|
||||
for (id, _) in remotes.into_iter() {
|
||||
let alice_repo = alice.repository(proj).unwrap();
|
||||
let alice_oid = alice_repo.reference(&id, refname).unwrap().unwrap();
|
||||
let alice_oid = alice_repo.reference(&id, &refname).unwrap().unwrap();
|
||||
|
||||
let bob_repo = bob.repository(proj).unwrap();
|
||||
let bob_oid = bob_repo.reference(&id, refname).unwrap().unwrap();
|
||||
let bob_oid = bob_repo.reference(&id, &refname).unwrap().unwrap();
|
||||
|
||||
assert_eq!(alice_oid.target(), bob_oid.target());
|
||||
}
|
||||
@@ -403,7 +407,7 @@ mod tests {
|
||||
let mut unsigned = project.references(&alice).unwrap();
|
||||
|
||||
// The signed refs doesn't contain the signature ref itself.
|
||||
unsigned.remove(SIGNATURE_REF).unwrap();
|
||||
unsigned.remove(&*SIGNATURE_REF).unwrap();
|
||||
|
||||
assert_eq!(remote.refs, signed);
|
||||
assert_eq!(*remote.refs, unsigned);
|
||||
|
||||
+16
-16
@@ -7,6 +7,7 @@ use std::ops::{Deref, DerefMut};
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use radicle_git_ext as git_ext;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
@@ -18,7 +19,7 @@ use crate::git::Oid;
|
||||
use crate::storage;
|
||||
use crate::storage::{ReadRepository, RemoteId, WriteRepository};
|
||||
|
||||
pub const SIGNATURE_REF: &str = "radicle/signature";
|
||||
pub static SIGNATURE_REF: Lazy<git::RefString> = Lazy::new(|| git::refname!("radicle/signature"));
|
||||
pub const REFS_BLOB_PATH: &str = "refs";
|
||||
pub const SIGNATURE_BLOB_PATH: &str = "signature";
|
||||
|
||||
@@ -51,7 +52,7 @@ pub enum Error {
|
||||
|
||||
/// The published state of a local repository.
|
||||
#[derive(Default, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Refs(BTreeMap<String, Oid>);
|
||||
pub struct Refs(BTreeMap<git::RefString, Oid>);
|
||||
|
||||
impl Refs {
|
||||
/// Verify the given signature on these refs, and return [`SignedRefs`] on success.
|
||||
@@ -100,7 +101,7 @@ impl Refs {
|
||||
.split_once(' ')
|
||||
.ok_or(canonical::Error::InvalidFormat)?;
|
||||
|
||||
let name = name.to_owned();
|
||||
let name = git::RefString::try_from(name)?;
|
||||
let oid = Oid::from_str(oid)?;
|
||||
|
||||
refs.insert(name, oid);
|
||||
@@ -110,7 +111,7 @@ impl Refs {
|
||||
|
||||
pub fn canonical(&self) -> Vec<u8> {
|
||||
let mut buf = String::new();
|
||||
let refs = self.iter().filter(|(name, _)| *name != SIGNATURE_REF);
|
||||
let refs = self.iter().filter(|(name, _)| *name != &*SIGNATURE_REF);
|
||||
|
||||
for (name, oid) in refs {
|
||||
buf.push_str(&oid.to_string());
|
||||
@@ -122,7 +123,7 @@ impl Refs {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Refs> for BTreeMap<String, Oid> {
|
||||
impl From<Refs> for BTreeMap<git::RefString, Oid> {
|
||||
fn from(refs: Refs) -> Self {
|
||||
refs.0
|
||||
}
|
||||
@@ -134,14 +135,14 @@ impl<V> From<SignedRefs<V>> for Refs {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BTreeMap<String, Oid>> for Refs {
|
||||
fn from(refs: BTreeMap<String, Oid>) -> Self {
|
||||
impl From<BTreeMap<git::RefString, Oid>> for Refs {
|
||||
fn from(refs: BTreeMap<git::RefString, Oid>) -> Self {
|
||||
Self(refs)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Refs {
|
||||
type Target = BTreeMap<String, Oid>;
|
||||
type Target = BTreeMap<git::RefString, Oid>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
@@ -196,7 +197,7 @@ impl SignedRefs<Verified> {
|
||||
where
|
||||
S: ReadRepository,
|
||||
{
|
||||
if let Some(oid) = repo.reference_oid(remote, SIGNATURE_REF)? {
|
||||
if let Some(oid) = repo.reference_oid(remote, &SIGNATURE_REF)? {
|
||||
Self::load_at(oid, remote, repo)
|
||||
} else {
|
||||
Err(Error::NotFound)
|
||||
@@ -233,8 +234,9 @@ impl SignedRefs<Verified> {
|
||||
remote: &RemoteId,
|
||||
repo: &S,
|
||||
) -> Result<Updated, Error> {
|
||||
let sigref = &*SIGNATURE_REF;
|
||||
let parent: Option<git2::Commit> = repo
|
||||
.reference(remote, SIGNATURE_REF)?
|
||||
.reference(remote, sigref)?
|
||||
.map(|r| r.peel_to_commit())
|
||||
.transpose()?;
|
||||
|
||||
@@ -260,13 +262,13 @@ impl SignedRefs<Verified> {
|
||||
}
|
||||
}
|
||||
|
||||
let sigref = format!("refs/remotes/{remote}/{SIGNATURE_REF}");
|
||||
let sigref = format!("refs/remotes/{remote}/{sigref}");
|
||||
let author = repo.raw().signature()?;
|
||||
let commit = repo.raw().commit(
|
||||
Some(&sigref),
|
||||
&author,
|
||||
&author,
|
||||
&format!("Update {} for {}", SIGNATURE_REF, remote),
|
||||
&format!("Update {} for {}", sigref, remote),
|
||||
&tree,
|
||||
&parent.iter().collect::<Vec<&git2::Commit>>(),
|
||||
);
|
||||
@@ -306,6 +308,8 @@ pub mod canonical {
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error(transparent)]
|
||||
InvalidRef(#[from] git_ref_format::Error),
|
||||
#[error("invalid canonical format")]
|
||||
InvalidFormat,
|
||||
#[error(transparent)]
|
||||
@@ -325,10 +329,6 @@ mod tests {
|
||||
let encoded = refs.canonical();
|
||||
let decoded = Refs::from_canonical(&encoded).unwrap();
|
||||
|
||||
println!("{:?}", refs);
|
||||
|
||||
assert_eq!(refs, decoded);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Test canonical/from_canonical
|
||||
|
||||
Reference in New Issue
Block a user