radicle/crefs/rules: Exact patterns for branches

Allows avoiding `try_from`, thus also get rid of one error variant.
This commit is contained in:
Lorenz Leutgeb 2025-09-16 14:41:27 +02:00 committed by Fintan Halpenny
parent bfb2858054
commit e822623dc1
2 changed files with 8 additions and 3 deletions

View File

@ -81,6 +81,13 @@ impl<'a> TryFrom<Qualified<'a>> for Pattern {
}
impl Pattern {
/// Construct a [`Pattern`] that matches a branch exactly.
///
/// The resulting [`Pattern`] will match `refs/heads/<name>`.
pub fn refs_heads_exact(name: &git::fmt::RefStr) -> Self {
Self(QualifiedPattern::from(git::refs::branch(name)))
}
/// Check if the `refname` matches the rule's `refspec`.
pub fn matches(&self, refname: &Qualified) -> bool {
// N.b. Git's refspecs do not quite match with glob-star semantics. A

View File

@ -76,8 +76,6 @@ impl DocError {
#[derive(Debug, Error)]
pub enum DefaultBranchRuleError {
#[error("could not create rule due to the reference name being invalid: {0}")]
Pattern(#[from] rules::PatternError),
#[error("could not load `xyz.radicle.project` to get default branch name: {0}")]
Payload(#[from] PayloadError),
}
@ -746,7 +744,7 @@ impl Doc {
pub fn default_branch_rule(
&self,
) -> Result<(rules::Pattern, rules::ValidRule), DefaultBranchRuleError> {
let pattern = rules::Pattern::try_from(self.default_branch()?.to_owned())?;
let pattern = rules::Pattern::refs_heads_exact(self.project()?.default_branch());
let rule = rules::Rule::new(
rules::ResolvedDelegates::Delegates(self.delegates.clone()),
self.threshold,