Answer a question

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?

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

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

Logo

ModelScope旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单!

更多推荐