如何将一个 github 分支规则应用于多个分支?

我的意思是,我想创建一个规则并指定多个分支,如 dev|master。但是看了医生之后,我觉得这是不可能的?为了使用相同的规则来保护两个分支,我必须创建两个规则吗?

40492 次浏览

Have also been trying to get my head around this this this morning, I believe you(/we) may have to create two identical rules for each branch oddly. At least that's what I believe after reading through:

https://github.community/t5/How-to-use-Git-and-GitHub/Apply-a-single-branch-protection-rule-to-both-master-and-release/td-p/11587

Comment from Moderator:

"No, there isn't a way to do that in the "Apply rule to" box. As stated in the protected branches documentation, we use the fnmatch library to match branch names to the match expression. There is a feature that would allow for matching two rules like that if there is a flag enabled but we don't enable that flag in our environment."

OR you could use this solution if you want to apply one rule to all branches beginning with or including the same matching phrase:

https://github.community/t5/How-to-use-Git-and-GitHub/Branch-Protection-on-multiple-branches/td-p/10519

Comment from Community Manager:

Branch protection rule patterns are based on fnmatch syntax. You could use releases/v?.? to automatically protect branches like releases/v1.0, releases/v2.0, and releases/v2.1. And [1-9]-[0-9]-stable could automatically protect branches like 1-0-stable, 2-0-stable, and 2-1-stable.

I found a rather ugly way to do this that at least gets in the ballpark (although it would be a lot better if @GitHub would give us something better than fnmatch with all options off...).

You can use character sets to specify the beginning characters in the repo name, like this:

(Using "main" branch): [dm][ea][vi]*
(Using "master" branch): [dm][ea][vs]*

It will match dev and main/master which is what you want, but the second one will also match "mastodon-rules" and "devo-is-my-favorite-band" due to the wildcard. I don't think fnmatch give you a "zero-or-one" quantifier like the regex ? so it's pretty restrictive.

Github fnmatch does allow the negation of a character set, so if a rule is catching branches you don't want to include, you might be able to get around that:

(using "main" branch): [dm][ea][vi][!o]*
(using "master" branch): [dm][ea][vs][!o]*

This will miss the dev branch (it will catch develop and main/master though...), but it excludes "devo" so at least 'whip it' won't start playing during your next all-night thrash session with your metalhead buddies.

Admittedly, this is not a very satisfying solution. But with fnmatch this might be the best option available.


What You Should Not Do

There are multiple other answers claiming that this pattern (or a similar variant) will work just fine:

[main,qa,stage,master]*

DO NOT BE LURED BY THIS SIRENS SONG

The engine treats characters enclosed in square [] brackets as just that: individual characters. Adding commas (or semicolons, or any other "separator") does not change that behavior.

Square Brackets: "match any one of the enclosed characters"
Star: "match any string of any length"

So, while this pattern will certainly match the words in the brackets, it will also match any string of any length that starts with one of the characters in the brackets: [aegimnqrst,].

According to the GitHub documentation, they use the fnmatch library for the pattern field. That syntax allows an alternation:

{a,b}

Matches pattern a and pattern b if File::FNM_EXTGLOB flag is enabled. Behaves like a Regexp union ((?:a|b)).

For your problem, the pattern you’re looking for might be {dev,master}.

I don’t know what they mean by “if File::FNM_EXTGLOB flag is enabled”, so this might not work.

They already enable wildcards. So this pattern works:

[main,qa,stage,master]*

For anyone that need a rule for covering only dev and main, its possible with this syntax:

https://ruby-doc.org/core-2.5.1/File.html#method-c-fnmatch

[cd]*[vd]

CONS

Will match with everithing that starts with c or d, and ends with v and d

Following z4-tear great explination This will cover development master and staging [dms][tea][avs]*[iet][ne][gtr]

Enable it on develop, master but not test

[develop;master]*[!test]*

to apply to all, add --> **/**