radicle: More convenience methods to get default branch

This commit is contained in:
Lorenz Leutgeb 2025-09-07 14:01:27 +02:00 committed by Fintan Halpenny
parent 22b2871f64
commit 0eba6caf9f
3 changed files with 14 additions and 4 deletions

View File

@ -737,12 +737,16 @@ impl Doc {
Ok(proj) Ok(proj)
} }
/// Gets the qualified reference name of the default branch,
/// according to the project payload in this document.
pub fn default_branch(&self) -> Result<git::fmt::Qualified<'_>, PayloadError> {
Ok(git::refs::branch(self.project()?.default_branch()))
}
pub fn default_branch_rule( pub fn default_branch_rule(
&self, &self,
) -> Result<(rules::Pattern, rules::ValidRule), DefaultBranchRuleError> { ) -> Result<(rules::Pattern, rules::ValidRule), DefaultBranchRuleError> {
let proj = self.project()?; let pattern = rules::Pattern::try_from(self.default_branch()?.to_owned())?;
let refname = proj.default_branch();
let pattern = rules::Pattern::try_from(git::refs::branch(refname).to_owned())?;
let rule = rules::Rule::new( let rule = rules::Rule::new(
rules::ResolvedDelegates::Delegates(self.delegates.clone()), rules::ResolvedDelegates::Delegates(self.delegates.clone()),
self.threshold, self.threshold,

View File

@ -520,6 +520,12 @@ pub trait ReadRepository: Sized + ValidateRepository {
/// Returns the [`Oid`] as well as the qualified reference name. /// Returns the [`Oid`] as well as the qualified reference name.
fn head(&self) -> Result<(Qualified<'_>, Oid), RepositoryError>; fn head(&self) -> Result<(Qualified<'_>, Oid), RepositoryError>;
/// Gets the qualified reference name of the default branch of self,
/// according to the project payload in the identity document.
fn default_branch(&self) -> Result<Qualified<'_>, RepositoryError> {
Ok(self.identity_doc()?.default_branch()?.to_owned())
}
/// Compute the canonical head of this repository. /// Compute the canonical head of this repository.
/// ///
/// Ignores any existing `HEAD` reference. /// Ignores any existing `HEAD` reference.

View File

@ -853,7 +853,7 @@ impl ReadRepository for Repository {
fn canonical_head(&self) -> Result<(Qualified<'_>, Oid), RepositoryError> { fn canonical_head(&self) -> Result<(Qualified<'_>, Oid), RepositoryError> {
let doc = self.identity_doc()?; let doc = self.identity_doc()?;
let refname = git::refs::branch(doc.project()?.default_branch()); let refname = doc.default_branch()?.to_owned();
let crefs = match doc.canonical_refs()? { let crefs = match doc.canonical_refs()? {
Some(crefs) => crefs, Some(crefs) => crefs,
// Fallback to constructing the default branch via the project // Fallback to constructing the default branch via the project