Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow specifying a filter for which branch names are allowed

  • Loading branch information...
commit 6dcab56995d04969f3d6f4e9b31be30f967dd74a 1 parent 127968b
@mhagander authored
Showing with 31 additions and 0 deletions.
  1. +11 −0 README.rst
  2. +20 −0 policyenforce.py
View
11 README.rst
@@ -94,6 +94,7 @@ configuration for the script. It should contain something like: ::
nolightweighttags=1
nobranchcreate=1
nobranchremove=1
+ branchnamefilter=REL_\d+$
[committers]
Example User=example@example.org
@@ -123,6 +124,16 @@ nobranchdelete
Enforce that existing branches cannot be removed (by pushing a
branch with the name :*branch*)
+There are also policies that should be set to a string:
+
+branchnamefilter
+ Set to a regular expression that will be applied to all new branches
+ created. If the expression matches, the branch creation will be
+ allowed, otherwise not. The expression will always be anchored at
+ the beginning, but if you want it anchored at the end you need to
+ add a $ at the end. Setting *nobranchcreate* will override this
+ setting and not allow any branches at all.
+
git command wrapper script
==========================
View
20 policyenforce.py
@@ -54,6 +54,20 @@ def _enforce(self, policyname):
except Exception,e:
return False
+ def _enforce_str(self, policyname):
+ """
+ Check if a specific policy should be enforced, returning a string
+ containing the value of the policy, or None if the policy is not
+ specified or empty.
+ """
+ try:
+ enf = c.get("policies", policyname).strip()
+ if enf == "":
+ return None
+ return enf
+ except Exception, e:
+ return None
+
class Commit(PolicyObject):
"""
@@ -180,6 +194,12 @@ def __init__(self, ref, name):
def check_create(self):
if self._enforce("nobranchcreate"):
self._policyfail("No branch creation allowed")
+ if self._enforce_str("branchnamefilter"):
+ # All branch names starts with refs/heads/, so just remove that
+ # when doing the regexp match
+ if not re.match(self._enforce_str("branchnamefilter"),
+ self.name[11:]):
+ self._policyfail("Branch name does not match allowed regexp")
def check_remove(self):
if self._enforce("nobranchdelete"):
Please sign in to comment.
Something went wrong with that request. Please try again.