Skip to content

Commit

Permalink
Add policy for refusing lightweight tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mhagander committed Sep 17, 2010
1 parent b8d3a46 commit c8763a9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.rst
Expand Up @@ -91,6 +91,7 @@ configuration for the script. It should contain something like: ::
nomerge=1
committerequalsauthor=1
committerlist=1
nolightweighttags=1
[committers]
Example User=example@example.org
Expand All @@ -111,6 +112,9 @@ committerlist
config file. This ensures that committers don't accidentally use a
badly configured client. All the commiters should be listed in the
[committers] section, in the format User Name=email.
nolightweighttags
Enforce that there are no lightweight tags - only tags carrying
a description are allowed.


git command wrapper script
Expand Down
54 changes: 39 additions & 15 deletions policyenforce.py
Expand Up @@ -41,8 +41,21 @@
print "Except: %s" % e
debug = 1

class PolicyObject(object):
def _enforce(self, policyname):
"""
Check if a specific policy should be enforced, returning True/False
"""
try:
enf = int(c.get("policies", policyname))
if enf == 1:
return True
return False
except Exception,e:
return False


class Commit(object):
class Commit(PolicyObject):
"""
This class wraps a single commit, and the checking of policies on it.
"""
Expand Down Expand Up @@ -97,18 +110,6 @@ def _parse_author(self, authorstring):
raise Exception("User '%s' on commit %s does not follow format rules." % (authorstring, self.commitid))
return m.group(1)

def _enforce(self, policyname):
"""
Check if a specific policy should be enforced, returning True/False
"""
try:
enf = int(c.get("policies", policyname))
if enf == 1:
return True
return False
except Exception,e:
return False

def _policyfail(self, msg):
"""
Indicate that a commit violated a policy, and abort the program with the
Expand Down Expand Up @@ -147,6 +148,30 @@ def check_policies(self):
# Currently no policy for "authorlist" - expect committerequalsauthor+committerlist to be
# used in those cases.


class Tag(PolicyObject):
def __init__(self, ref, name):
self.ref = ref
self.name = name

def check_policies(self):
if self._enforce("nolightweighttag"):
# A lightweight tag is a tag object, a "heavy" tag is
# a commit object.
p = Popen("git cat-file -t %s" % self.ref, shell=True, stdout=PIPE)
t = p.stdout.read().strip()
p.stdout.close()
if t == "commit":
self._policyfail("No lightweight tags allowed")

def _policyfail(self, msg):
"""
Indicate that a tag violated a policy, and abort the program with the
appropriate exitcode.
"""
print "Tag %s violates the policy: %s" % (self.name, msg)
sys.exit(1)

if __name__ == "__main__":
# Get a list of refs on stdin, do something smart with it
ref = sys.argv[1]
Expand All @@ -161,8 +186,7 @@ def check_policies(self):
pass
elif ref.startswith("refs/tags/"):
# It's a tag!
# We currently have no policies to enforce on this.
pass
Tag(newobj, ref).check_policies()
else:
raise Exception("Unknown branch/tag type %s" % ref)

Expand Down

0 comments on commit c8763a9

Please sign in to comment.