svartalf is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againmisc / mercurial_hooks.py
- Branch
- default
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | # -*- coding: utf-8 -*-
"""
Примеры хуков для mercurial
"""
import os
import re
from mercurial import patch
def print_check(ui, repo, **kwargs):
"""Отмена коммита, если в коде есть print
Подключение в .hgrc:
precommit = python:.hg/hooks.py:print_check"""
linenum, header = 0, False
files = {}
diff = patch.diff(repo)
for num, filediff in enumerate(diff):
for line in filediff.split('\n'):
if header:
m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line)
if m and m.group(1) != '/dev/null':
filename = m.group(1).split('/', 1)[-1]
if line.startswith('+++ '):
header = False
continue
if line.startswith('diff '):
header = True
continue
m = re.match(r'@@ -\d+,\d+ \+(\d+),', line)
if m:
linenum = int(m.group(1))
continue
m = re.match(r'^(?:-|\+)\s*print', line)
if m:
if not files.has_key(filename):
files[filename] = []
files[filename].append((linenum, line))
if line and line[0] in ' +':
linenum += 1
if files:
ui.warn('Error! Print statements founded\n')
for file in files:
ui.warn('\nFile: %s\n' % file)
for error in files[file]:
ui.warn('\tLine %s: %s\n' % (error[0], error[1].strip(' -+')[:70]))
return bool(files)
def console_check(ui, repo, **kwargs):
"""Отмена коммита, если в .js файлах есть вызов console.*
Подключение в .hgrc:
precommit = python:.hg/hooks.py:console_check"""
linenum, header = 0, False
files = {}
diff = patch.diff(repo)
for num, filediff in enumerate(diff):
for line in filediff.split('\n'):
if header:
m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line)
if m and m.group(1) != '/dev/null':
filename = m.group(1).split('/', 1)[-1]
if line.startswith('+++ '):
header = False
continue
if line.startswith('diff '):
header = True
continue
if not os.path.splitext(filename)[1] in ('.js', '.html'):
continue
m = re.match(r'@@ -\d+,\d+ \+(\d+),', line)
if m:
linenum = int(m.group(1))
continue
m = re.match(r'^(?:-|\+)\s*console.', line)
if m:
if not files.has_key(filename):
files[filename] = []
files[filename].append((linenum, line))
if line and line[0] in ' +':
linenum += 1
if files:
ui.warn('Error! JS console statements founded\n')
for file in files:
ui.warn('\nFile: %s\n' % file)
for error in files[file]:
ui.warn('\tLine %s: %s\n' % (error[0], error[1].strip(' -+')[:70]))
def clean_cache(ui, repo, **kwargs):
"""После обновления репозитория удаляем .pyc файлы
Подключение в .hgrc:
preupdate = python:.hg/hooks.py:clean_cache"""
for root, dirs, files in os.walk(repo.root):
for file in files:
if os.path.splitext(file)[1] == '.pyc':
os.remove(os.path.join(root, file))
|