Skip to content
refs
History
27 lines · 877 BRaw
1/** A branch glob: `*` matches within one path segment, `**` matches across segments. Everything else is literal. */
2export class BranchPattern {
3 readonly source: string;
4 readonly #regex: RegExp;
5
6 constructor(source: string) {
7 this.source = source;
8 let body = "";
9 for (let i = 0; i < source.length; i++) {
10 const char = source[i];
11 if (char === "*" && source[i + 1] === "*") {
12 body += ".*";
13 i++;
14 } else if (char === "*") body += "[^/]*";
15 else body += char.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
16 }
17 this.#regex = new RegExp(`^${body}$`);
18 }
19
20 matches(branch: string): boolean {
21 return this.#regex.test(branch);
22 }
23}
24
25export function matchesAny(patterns: readonly string[], branch: string): boolean {
26 return patterns.length === 0 || patterns.some((pattern) => new BranchPattern(pattern).matches(branch));
27}