#!/usr/bin/env python2 # vim:fileencoding=utf-8 et st=4 sts=4 # Copyright 2012-2014 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Id$ # Author: Michał Górny # Author: Robin H. Johnson (hook fixups) # Based on work of: Ulrich Müller import datetime, os, re, subprocess, sys, fileinput PATH_REGEX = re.compile(r''' (?P \d{4}) - (?P \d{2}) - (?P \d{2}) - (?P [a-z0-9+_-]+) / (?P=year) - (?P=month) - (?P=day) - (?P=name) . (?P [a-z]{2}) .txt (?: .asc )? ''', re.VERBOSE) # special git commit id ZEROS = '0000000000000000000000000000000000000000' def main(prog, *argv): if 'GIT_DIR' not in os.environ: return '%s: GIT_DIR unset' % (prog, ) results = [] for line in fileinput.input(): # SP SP LF (oldrev, newrev, refname) = line.split() results += [validate(oldrev, newrev, refname)] results = filter(lambda x: x != 0, results) if len(results) == 0: return 0 else: print '%s: errors in commits' % (prog, ) print ''.join(map(lambda x: x+"\n", results)) return 1 def validate(oldrev, newrev, refname): # Deletion of a branch means no work to do anyway. # And git-diff will fail if newrev == ZEROS: return 0 proc = subprocess.Popen( \ ['git', 'diff', \ '--name-only', '--diff-filter=A', \ '%s..%s' % (oldrev, newrev)], stdout=subprocess.PIPE) added = proc.communicate()[0].rstrip().split('\n') for filename in filter(lambda f: len(f) > 0, added): # GLEP 42: name should take the form of: # yyyy-mm-dd-/yyyy-mm-dd-..txt[.asc] # where : [a-z0-9+_-]+, : [a-z][a-z] match = PATH_REGEX.match(filename) if not match: return 'Path syntax invalid: %s' % filename date = [int(match.group(x)) for x in ('year', 'month', 'day')] try: datetime.date(*date) except ValueError: return 'Date invalid: %s' % '-'.join(date) # return nothing if good return 0 if __name__ == '__main__': sys.exit(main(*sys.argv))