Skip to content

Commit

Permalink
Allow specifying a filter for which branch names are allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
mhagander committed Jun 28, 2011
1 parent 127968b commit 6dcab56
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
==========================
Expand Down
20 changes: 20 additions & 0 deletions policyenforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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"):
Expand Down

0 comments on commit 6dcab56

Please sign in to comment.