From 0eba6caf9f09cc0186ecf9ba0520664766a3b7d4 Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Sun, 7 Sep 2025 14:01:27 +0200 Subject: [PATCH] radicle: More convenience methods to get default branch --- crates/radicle/src/identity/doc.rs | 10 +++++++--- crates/radicle/src/storage.rs | 6 ++++++ crates/radicle/src/storage/git.rs | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/radicle/src/identity/doc.rs b/crates/radicle/src/identity/doc.rs index f017c9b3..22b304e3 100644 --- a/crates/radicle/src/identity/doc.rs +++ b/crates/radicle/src/identity/doc.rs @@ -737,12 +737,16 @@ impl Doc { 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, PayloadError> { + Ok(git::refs::branch(self.project()?.default_branch())) + } + pub fn default_branch_rule( &self, ) -> Result<(rules::Pattern, rules::ValidRule), DefaultBranchRuleError> { - let proj = self.project()?; - let refname = proj.default_branch(); - let pattern = rules::Pattern::try_from(git::refs::branch(refname).to_owned())?; + let pattern = rules::Pattern::try_from(self.default_branch()?.to_owned())?; let rule = rules::Rule::new( rules::ResolvedDelegates::Delegates(self.delegates.clone()), self.threshold, diff --git a/crates/radicle/src/storage.rs b/crates/radicle/src/storage.rs index 61cefda9..3315b13e 100644 --- a/crates/radicle/src/storage.rs +++ b/crates/radicle/src/storage.rs @@ -520,6 +520,12 @@ pub trait ReadRepository: Sized + ValidateRepository { /// Returns the [`Oid`] as well as the qualified reference name. 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, RepositoryError> { + Ok(self.identity_doc()?.default_branch()?.to_owned()) + } + /// Compute the canonical head of this repository. /// /// Ignores any existing `HEAD` reference. diff --git a/crates/radicle/src/storage/git.rs b/crates/radicle/src/storage/git.rs index 7d00c044..ad865215 100644 --- a/crates/radicle/src/storage/git.rs +++ b/crates/radicle/src/storage/git.rs @@ -853,7 +853,7 @@ impl ReadRepository for Repository { fn canonical_head(&self) -> Result<(Qualified<'_>, Oid), RepositoryError> { 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()? { Some(crefs) => crefs, // Fallback to constructing the default branch via the project