Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply one github branch rule to multiple branches?

Tags:

github

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?

like image 542
Sraw Avatar asked Nov 03 '18 20:11

Sraw


People also ask

What are GitHub branch protection rules?

Git branch protection rules are a powerful configuration option that enables repository administrators to enforce security policies. This helps protect the git branches from unexpected code commits or deletion by any unauthorized person(s) / user group(s).

Can we have one main branch and multiple branches in a single repository?

Each repository can have one or more branches. The main branch — the one where all changes eventually get merged back into, and is called master. This is the official working version of your project, and the one you see when you visit the project repository at github.com/yourname/projectname.


1 Answers

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,].

like image 74
Z4-tier Avatar answered Oct 06 '22 09:10

Z4-tier