##// END OF EJS Templates
contrib: add check-commit hook script to sanity-check commits
Matt Mackall -
r22043:1274ff3f default
parent child Browse files
Show More
@@ -0,0 +1,54 b''
1 #!/usr/bin/env python
2 #
3 # Copyright 2014 Matt Mackall <mpm@selenic.com>
4 #
5 # A tool/hook to run basic sanity checks on commits/patches for
6 # submission to Mercurial. Install by adding the following to your
7 # .hg/hgrc:
8 #
9 # [hooks]
10 # pretxncommit = contrib/check-commit
11 #
12 # The hook can be temporarily bypassed with:
13 #
14 # $ BYPASS= hg commit
15 #
16 # See also: http://mercurial.selenic.com/wiki/ContributingChanges
17
18 import re, sys, os
19
20 errors = [
21 (r"[(]bc[)]", "(BC) needs to be uppercase"),
22 (r"[(]issue \d\d\d", "no space allowed between issue and number"),
23 (r"[(]bug", "use (issueDDDD) instead of bug"),
24 (r"^# User [^@\n]+$", "username is not an email address"),
25 (r"^# .*\n(?!merge with )[^#]\S+[^:] ",
26 "summary line doesn't start with 'topic: '"),
27 (r"^# .*\n[A-Z][a-z]\S+", "don't capitalize summary lines"),
28 (r"^# .*\n.*\.\s+$", "don't add trailing period on summary line"),
29 (r"^# .*\n.{78,}", "summary line too long"),
30 (r"\+\s+def [a-z]+_[a-z]", "adds a function with foo_bar naming"),
31 ]
32
33 node = os.environ.get("HG_NODE")
34
35 if node:
36 commit = os.popen("hg export %s" % node).read()
37 else:
38 commit = sys.stdin.read()
39
40 exitcode = 0
41 for exp, msg in errors:
42 m = re.search(exp, commit, re.MULTILINE)
43 if m:
44 pos = 0
45 for n, l in enumerate(commit.splitlines(True)):
46 pos += len(l)
47 if pos >= m.end():
48 print "%d: %s" % (n, msg)
49 print " %s" % l[:-1]
50 if "BYPASS" not in os.environ:
51 exitcode = 1
52 break
53
54 sys.exit(exitcode)
General Comments 0
You need to be logged in to leave comments. Login now