Document that disabledBranch config supports negative lookahead
A regular expression with a negative lookahead [1] can be used to match
all but one branches. E.g. this can be used to enable the code owners
functionality only for the master branch.
Cover this by a test and document this, so that this feature is easier
to discover.
[1] https://www.regular-expressions.info/lookaround.html
Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I3fd464987eb626dae72cadcf9d536975e90e0532
diff --git a/javatests/com/google/gerrit/plugins/codeowners/config/StatusConfigTest.java b/javatests/com/google/gerrit/plugins/codeowners/config/StatusConfigTest.java
index e8eff5f..6ad8f3b 100644
--- a/javatests/com/google/gerrit/plugins/codeowners/config/StatusConfigTest.java
+++ b/javatests/com/google/gerrit/plugins/codeowners/config/StatusConfigTest.java
@@ -192,6 +192,34 @@
}
@Test
+ public void isDisabledForBranch_regularExpressionWithNegativeLookahead() throws Exception {
+ Config cfg = new Config();
+ cfg.setStringList(
+ SECTION_CODE_OWNERS,
+ null,
+ KEY_DISABLED_BRANCH,
+ // match all branches except refs/heads/master
+ ImmutableList.of("^refs/(?!heads/master$).*"));
+ assertThat(statusConfig.isDisabledForBranch(cfg, BranchNameKey.create(project, "master")))
+ .isFalse();
+ assertThat(
+ statusConfig.isDisabledForBranch(cfg, BranchNameKey.create(project, "master-foo-bar")))
+ .isTrue();
+ assertThat(statusConfig.isDisabledForBranch(cfg, BranchNameKey.create(project, "foo")))
+ .isTrue();
+ assertThat(statusConfig.isDisabledForBranch(cfg, BranchNameKey.create(project, "other")))
+ .isTrue();
+ assertThat(
+ statusConfig.isDisabledForBranch(
+ cfg, BranchNameKey.create(project, RefNames.REFS_CONFIG)))
+ .isTrue();
+ assertThat(
+ statusConfig.isDisabledForBranch(
+ cfg, BranchNameKey.create(project, "refs/meta/master")))
+ .isTrue();
+ }
+
+ @Test
@GerritConfig(name = "plugin.code-owners.disabledBranch", value = "refs/heads/master")
public void isDisabledForBranchIsRetrievedFromGerritConfigIfNotSpecifiedOnProjectLevel()
throws Exception {
diff --git a/resources/Documentation/config.md b/resources/Documentation/config.md
index f1f1c24..5f3724c 100644
--- a/resources/Documentation/config.md
+++ b/resources/Documentation/config.md
@@ -24,6 +24,10 @@
<a id="pluginCodeOwnersDisabledBranch">plugin.@PLUGIN@.disabledBranch</a>
: An exact ref, a ref pattern or a regular expression to disable the code
owners functionality for the matched branches.\
+ By using a negative lookahead, it's possible to match all but one
+ branches. E.g. to disable the code owners functionality for all branches
+ except the `refs/heads/master` branch the following regular expression
+ can be used: `^refs/(?!heads/master$).*`
For matched branches submitting changes doesn't require code owner
approvals.\
This allows branches to opt-out of the code owners functionality.\
@@ -219,6 +223,10 @@
<a id="codeOwnersDisabledBranch">codeOwners.disabledBranch</a>
: An exact ref, a ref pattern or a regular expression to disable the code
owners functionality for the matched branches.\
+ By using a negative lookahead, it's possible to match all but one
+ branches. E.g. to disable the code owners functionality for all branches
+ except the `refs/heads/master` branch the following regular expression
+ can be used: `^refs/(?!heads/master$).*`
For matched branches submitting changes doesn't require code owner
approvals.\
This allows branches to opt-out of the code owners functionality.\