From d3d706bbf73671f8fa4c91525c31695b9b027db1 Mon Sep 17 00:00:00 2001 From: Slack Coder Date: Tue, 20 Sep 2022 10:08:28 -0500 Subject: [PATCH] git: define common refs Add helper methods to improve source code clarity and prevent bugs due to typos. Signed-off-by: Slack Coder --- node/src/git.rs | 39 +++++++++++++++++++++++++++++++++++++++ node/src/identity/doc.rs | 8 ++++---- node/src/rad.rs | 22 ++++++++++++---------- 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/node/src/git.rs b/node/src/git.rs index b38c560f..c8bdd9fd 100644 --- a/node/src/git.rs +++ b/node/src/git.rs @@ -2,6 +2,7 @@ use std::path::Path; use std::str::FromStr; use git_ref_format as format; +use once_cell::sync::Lazy; use crate::collections::HashMap; use crate::crypto::PublicKey; @@ -35,6 +36,44 @@ pub enum ListRefsError { InvalidRef(#[from] RefError), } +pub mod refs { + use super::*; + + /// Where project information is kept. + pub static IDENTITY_BRANCH: Lazy = Lazy::new(|| refname!("radicle/id")); + + pub mod storage { + use super::*; + + pub fn branch(remote: &RemoteId, branch: &str) -> String { + format!("refs/remotes/{remote}/heads/{branch}") + } + + /// Get the branch used to track project information. + pub fn id(remote: &RemoteId) -> String { + branch(remote, &IDENTITY_BRANCH) + } + } + + pub mod workdir { + pub fn branch(branch: &str) -> String { + format!("refs/heads/{branch}") + } + + pub fn note(name: &str) -> String { + format!("refs/notes/{name}") + } + + pub fn remote_branch(remote: &str, branch: &str) -> String { + format!("refs/remotes/{remote}/{branch}") + } + + pub fn tag(name: &str) -> String { + format!("refs/tags/{name}") + } + } +} + /// List remote refs of a project, given the remote URL. pub fn remote_refs(url: &Url) -> Result, ListRefsError> { let url = url.to_string(); diff --git a/node/src/identity/doc.rs b/node/src/identity/doc.rs index 71783ea3..3c6b0a85 100644 --- a/node/src/identity/doc.rs +++ b/node/src/identity/doc.rs @@ -27,7 +27,6 @@ pub struct Untrusted; #[derive(Clone, Copy, Debug)] pub struct Trusted; -pub static REFERENCE_NAME: Lazy = Lazy::new(|| git::refname!("heads/radicle/id")); pub static PATH: Lazy<&Path> = Lazy::new(|| Path::new("radicle.json")); pub const MAX_STRING_LENGTH: usize = 255; @@ -155,7 +154,7 @@ impl Doc { let (_, doc) = self.encode()?; let tree = git::write_tree(*PATH, doc.as_slice(), repo.raw())?; - let id_ref = format!("refs/remotes/{remote}/{}", &*REFERENCE_NAME); + let id_ref = git::refs::storage::id(remote); let head = repo.raw().find_reference(&id_ref)?.peel_to_commit()?; let oid = Doc::commit(remote, &tree, &msg, &[&head], repo.raw())?; @@ -173,7 +172,7 @@ impl Doc { .signature() .or_else(|_| git2::Signature::now("radicle", remote.to_string().as_str()))?; - let id_ref = format!("refs/remotes/{remote}/{}", &*REFERENCE_NAME); + let id_ref = git::refs::storage::id(remote); let oid = repo.commit(Some(&id_ref), &sig, &sig, msg, tree, parents)?; Ok(oid.into()) @@ -352,7 +351,8 @@ impl Doc { remote: &RemoteId, repo: &R, ) -> Result, git::Error> { - if let Some(oid) = repo.reference_oid(remote, &REFERENCE_NAME)? { + let head = &git::refname!("heads").join(&*git::refs::IDENTITY_BRANCH); + if let Some(oid) = repo.reference_oid(remote, head)? { Ok(Some(oid)) } else { Ok(None) diff --git a/node/src/rad.rs b/node/src/rad.rs index f8a45549..815f1707 100644 --- a/node/src/rad.rs +++ b/node/src/rad.rs @@ -61,7 +61,7 @@ pub fn init<'r, G: Signer, S: storage::WriteStorage<'r>>( repo, REMOTE_NAME, &default_branch, - &format!("refs/remotes/{pk}/heads/{default_branch}"), + &git::refs::storage::branch(pk, &default_branch), )?; // TODO: Note that you'll likely want to use `RemoteCallbacks` and set @@ -69,7 +69,9 @@ pub fn init<'r, G: Signer, S: storage::WriteStorage<'r>>( // successfully. git::configure_remote(repo, REMOTE_NAME, pk, project.path())?.push::<&str>( &[&format!( - "refs/heads/{default_branch}:refs/remotes/{pk}/heads/{default_branch}" + "{}:{}", + &git::refs::workdir::branch(&default_branch), + &git::refs::storage::branch(pk, &default_branch), )], None, )?; @@ -114,25 +116,25 @@ pub fn fork<'r, G: Signer, S: storage::WriteStorage<'r>>( let raw = repository.raw(); let remote_head = raw - .find_reference(&format!( - "refs/remotes/{remote}/heads/{}", - &project.doc.default_branch + .find_reference(&git::refs::storage::branch( + remote, + &project.doc.default_branch, ))? .target() .ok_or(ForkError::InvalidReference)?; raw.reference( - &format!("refs/remotes/{me}/heads/{}", &project.doc.default_branch), + &git::refs::storage::branch(me, &project.doc.default_branch), remote_head, false, &format!("creating default branch for {me}"), )?; let remote_id = raw - .find_reference(&format!("refs/remotes/{remote}/heads/radicle/id"))? + .find_reference(&git::refs::storage::id(remote))? .target() .ok_or(ForkError::InvalidReference)?; raw.reference( - &format!("refs/remotes/{me}/heads/radicle/id"), + &git::refs::storage::id(me), remote_id, false, &format!("creating identity branch for {me}"), @@ -182,7 +184,7 @@ pub fn checkout, S: storage::ReadStorage>( { // Setup default branch. - let remote_head_ref = format!("refs/remotes/{REMOTE_NAME}/{default_branch}"); + let remote_head_ref = git::refs::workdir::remote_branch(REMOTE_NAME, default_branch); let remote_head_commit = repo.find_reference(&remote_head_ref)?.peel_to_commit()?; let _ = repo.branch(default_branch, &remote_head_commit, true)?; @@ -191,7 +193,7 @@ pub fn checkout, S: storage::ReadStorage>( &repo, REMOTE_NAME, default_branch, - &format!("refs/remotes/{remote}/heads/{default_branch}"), + &git::refs::storage::branch(remote, default_branch), )?; }