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 std::str::FromStr;
|
||||||
|
|
||||||
use git_ref_format as format;
|
use git_ref_format as format;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use crate::collections::HashMap;
|
use crate::collections::HashMap;
|
||||||
use crate::crypto::PublicKey;
|
use crate::crypto::PublicKey;
|
||||||
|
|
@ -35,6 +36,44 @@ pub enum ListRefsError {
|
||||||
InvalidRef(#[from] RefError),
|
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.
|
/// List remote refs of a project, given the remote URL.
|
||||||
pub fn remote_refs(url: &Url) -> Result<HashMap<RemoteId, Refs>, ListRefsError> {
|
pub fn remote_refs(url: &Url) -> Result<HashMap<RemoteId, Refs>, ListRefsError> {
|
||||||
let url = url.to_string();
|
let url = url.to_string();
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ pub struct Untrusted;
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub struct Trusted;
|
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 static PATH: Lazy<&Path> = Lazy::new(|| Path::new("radicle.json"));
|
||||||
|
|
||||||
pub const MAX_STRING_LENGTH: usize = 255;
|
pub const MAX_STRING_LENGTH: usize = 255;
|
||||||
|
|
@ -155,7 +154,7 @@ impl Doc<Verified> {
|
||||||
|
|
||||||
let (_, doc) = self.encode()?;
|
let (_, doc) = self.encode()?;
|
||||||
let tree = git::write_tree(*PATH, doc.as_slice(), repo.raw())?;
|
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 head = repo.raw().find_reference(&id_ref)?.peel_to_commit()?;
|
||||||
let oid = Doc::commit(remote, &tree, &msg, &[&head], repo.raw())?;
|
let oid = Doc::commit(remote, &tree, &msg, &[&head], repo.raw())?;
|
||||||
|
|
||||||
|
|
@ -173,7 +172,7 @@ impl Doc<Verified> {
|
||||||
.signature()
|
.signature()
|
||||||
.or_else(|_| git2::Signature::now("radicle", remote.to_string().as_str()))?;
|
.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)?;
|
let oid = repo.commit(Some(&id_ref), &sig, &sig, msg, tree, parents)?;
|
||||||
|
|
||||||
Ok(oid.into())
|
Ok(oid.into())
|
||||||
|
|
@ -352,7 +351,8 @@ impl<V> Doc<V> {
|
||||||
remote: &RemoteId,
|
remote: &RemoteId,
|
||||||
repo: &R,
|
repo: &R,
|
||||||
) -> Result<Option<Oid>, git::Error> {
|
) -> 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))
|
Ok(Some(oid))
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ pub fn init<'r, G: Signer, S: storage::WriteStorage<'r>>(
|
||||||
repo,
|
repo,
|
||||||
REMOTE_NAME,
|
REMOTE_NAME,
|
||||||
&default_branch,
|
&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
|
// 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.
|
// successfully.
|
||||||
git::configure_remote(repo, REMOTE_NAME, pk, project.path())?.push::<&str>(
|
git::configure_remote(repo, REMOTE_NAME, pk, project.path())?.push::<&str>(
|
||||||
&[&format!(
|
&[&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,
|
None,
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -114,25 +116,25 @@ pub fn fork<'r, G: Signer, S: storage::WriteStorage<'r>>(
|
||||||
|
|
||||||
let raw = repository.raw();
|
let raw = repository.raw();
|
||||||
let remote_head = raw
|
let remote_head = raw
|
||||||
.find_reference(&format!(
|
.find_reference(&git::refs::storage::branch(
|
||||||
"refs/remotes/{remote}/heads/{}",
|
remote,
|
||||||
&project.doc.default_branch
|
&project.doc.default_branch,
|
||||||
))?
|
))?
|
||||||
.target()
|
.target()
|
||||||
.ok_or(ForkError::InvalidReference)?;
|
.ok_or(ForkError::InvalidReference)?;
|
||||||
raw.reference(
|
raw.reference(
|
||||||
&format!("refs/remotes/{me}/heads/{}", &project.doc.default_branch),
|
&git::refs::storage::branch(me, &project.doc.default_branch),
|
||||||
remote_head,
|
remote_head,
|
||||||
false,
|
false,
|
||||||
&format!("creating default branch for {me}"),
|
&format!("creating default branch for {me}"),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let remote_id = raw
|
let remote_id = raw
|
||||||
.find_reference(&format!("refs/remotes/{remote}/heads/radicle/id"))?
|
.find_reference(&git::refs::storage::id(remote))?
|
||||||
.target()
|
.target()
|
||||||
.ok_or(ForkError::InvalidReference)?;
|
.ok_or(ForkError::InvalidReference)?;
|
||||||
raw.reference(
|
raw.reference(
|
||||||
&format!("refs/remotes/{me}/heads/radicle/id"),
|
&git::refs::storage::id(me),
|
||||||
remote_id,
|
remote_id,
|
||||||
false,
|
false,
|
||||||
&format!("creating identity branch for {me}"),
|
&format!("creating identity branch for {me}"),
|
||||||
|
|
@ -182,7 +184,7 @@ pub fn checkout<P: AsRef<Path>, S: storage::ReadStorage>(
|
||||||
|
|
||||||
{
|
{
|
||||||
// Setup default branch.
|
// 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 remote_head_commit = repo.find_reference(&remote_head_ref)?.peel_to_commit()?;
|
||||||
let _ = repo.branch(default_branch, &remote_head_commit, true)?;
|
let _ = repo.branch(default_branch, &remote_head_commit, true)?;
|
||||||
|
|
||||||
|
|
@ -191,7 +193,7 @@ pub fn checkout<P: AsRef<Path>, S: storage::ReadStorage>(
|
||||||
&repo,
|
&repo,
|
||||||
REMOTE_NAME,
|
REMOTE_NAME,
|
||||||
default_branch,
|
default_branch,
|
||||||
&format!("refs/remotes/{remote}/heads/{default_branch}"),
|
&git::refs::storage::branch(remote, default_branch),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue