git: define common refs
Add helper methods to improve source code clarity and prevent bugs due to typos. Signed-off-by: Slack Coder <slackcoder@server.ky>
This commit is contained in:
parent
518e625d55
commit
d3d706bbf7
|
|
@ -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<RefString> = 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<HashMap<RemoteId, Refs>, ListRefsError> {
|
||||
let url = url.to_string();
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ pub struct Untrusted;
|
|||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Trusted;
|
||||
|
||||
pub static REFERENCE_NAME: Lazy<git::RefString> = 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<Verified> {
|
|||
|
||||
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<Verified> {
|
|||
.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<V> Doc<V> {
|
|||
remote: &RemoteId,
|
||||
repo: &R,
|
||||
) -> Result<Option<Oid>, 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)
|
||||
|
|
|
|||
|
|
@ -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<P: AsRef<Path>, 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<P: AsRef<Path>, S: storage::ReadStorage>(
|
|||
&repo,
|
||||
REMOTE_NAME,
|
||||
default_branch,
|
||||
&format!("refs/remotes/{remote}/heads/{default_branch}"),
|
||||
&git::refs::storage::branch(remote, default_branch),
|
||||
)?;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue