use globset::{Glob, GlobMatcher}; use crate::share::errors::GitError; #[derive(Debug, Default)] pub struct BranchPolicy { patterns: Vec, } impl BranchPolicy { pub fn from_patterns(patterns: impl IntoIterator) -> Result { let mut out = Vec::new(); for p in patterns { let glob = Glob::new(&p).map_err(|e| { GitError::InvalidArgument(format!("invalid branch pattern `{p}`: {e}")) })?; out.push(glob.compile_matcher()); } Ok(Self { patterns: out }) } pub fn is_empty(&self) -> bool { self.patterns.is_empty() } pub fn is_match(&self, name: &str) -> bool { self.patterns.iter().any(|m| m.is_match(name)) } } /// 分支保护模式,全部来自 KenShell 的保护规则。 pub async fn load_branch_policy( svc: &crate::service::GitService, ctx: &crate::context::facade::GitContext, ) -> Result { let rows = svc .baby(ctx) .list_ref_protections("branch") .await .map_err(crate::share::errors::map_baby)?; BranchPolicy::from_patterns(rows.into_iter().map(|view| view.pattern)) } pub async fn load_tag_policy( svc: &crate::service::GitService, ctx: &crate::context::facade::GitContext, ) -> Result { let rows = svc .baby(ctx) .list_ref_protections("tag") .await .map_err(crate::share::errors::map_baby)?; BranchPolicy::from_patterns(rows.into_iter().map(|view| view.pattern)) }