How to apply one github branch rule to multiple branches?

Github

Github Problem Overview


I mean I want to create one rule and specify multiple branches like dev|master. But after seeing the doc, I think it is impossible?? Do I have to create two rules just in order to use the same rule to protect two branches?

Github Solutions


Solution 1 - Github

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

One of the other answers here claims that this pattern works 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 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,].

Solution 2 - Github

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.

Solution 3 - Github

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

Solution 4 - Github

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

Solution 5 - Github

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.

Solution 6 - Github

They already enable wildcards. So this pattern works:

[main,qa,stage,master]*

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSrawView Question on Stackoverflow
Solution 1 - GithubZ4-tierView Answer on Stackoverflow
Solution 2 - GithubfoakesmView Answer on Stackoverflow
Solution 3 - Githubdandush03View Answer on Stackoverflow
Solution 4 - GithubAntony FagundezView Answer on Stackoverflow
Solution 5 - GithubchharveyView Answer on Stackoverflow
Solution 6 - GithubalacretView Answer on Stackoverflow