hg_hooks/filecheck/filecheck.py
author Simon Howkins <simonh@symbian.org>
Fri, 06 Nov 2009 12:10:52 +0000
changeset 99 5adea361d896
parent 22 f55ca49f7f44
permissions -rw-r--r--
Bug 881: "Automatic creation of release note content broken by config change" Added a new option to allow the caller to specify where the publish location is. Minor improvements to formatting of output. Turned on warnings, and fixed most of those that appeared. Greatly simplified the checking of the script arguments. Removed code which was noted to be unnecessary, and was. Declared some variables more closely to the code that uses them.

# filecheck.py - changeset filename check for mercurial
#
# should be configured as pretxnchangegroup to vet incoming changesets
# and as pretxncommit to vet local commit
#
# will receive a group of changesets from provided node to tip

from mercurial import util
from mercurial.i18n import _
import re

badpatterns = ('.*\.ttt\s*$','c.ttf','.*\.ttf\s*$','.*\.bbb\s*$',
               '.*\.bbb\s*$','.*\.ccc\s*$','.*\.ddd\s*$','.*\.eee\s*$','.*\.fff\s*$','.*\.ggg\s*$')

badexpr=[]
runonce=0

def init():
    global badexpr
    for p in badpatterns:
        badexpr.append((re.compile((p),re.IGNORECASE)))

def deny(f):
    global runonce
    if (not runonce):
        init()
        runonce =1
      
    for pat in badexpr:
        if(pat.match(f)):
            return(1)
    
    return(0)

def push_hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    if hooktype != 'pretxnchangegroup':
        raise util.Abort(_('config error - hook type "%s" cannot stop '
                           'incoming changesets') % hooktype)
    
    # iterate over all the added changesets between node and tip
    for rev in xrange(repo[node], len(repo)):
        ctx = repo[rev]
        for f in ctx.files():
            if deny(f):
                ui.debug(_('filecheck: file %s not allowed \n') % (f))
                raise util.Abort(_('filecheck: access denied for changeset %s file %s blocked') % (ctx,f))
        ui.debug(_('filecheck: allowing changeset %s\n') % ctx)

def commit_hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    # iterate over all the files in added changeset
    ctx = repo[node]
    for f in ctx.files():
        if deny(f):
            ui.debug(_('filecheck: file %s not allowed \n') % (f))
            raise util.Abort(_('filecheck: access denied for changeset %s file %s blocked') % (ctx,f))
    ui.debug(_('filecheck: allowing changeset %s\n') % ctx)