hgbook

view en/examples/data/check_whitespace.py @ 49:18210d46491f

More hook content.
author Bryan O'Sullivan <bos@serpentine.com>
date Tue Jul 25 00:18:31 2006 -0700 (2006-07-25)
parents 012df94a02fe
children e3894bb9d1f5
line source
1 #!/usr/bin/python
3 import re
5 def trailing_whitespace(difflines):
6 added, linenum, header = [], 0, False
8 for line in difflines:
9 if header:
10 if line.startswith('+++ '):
11 header = False
12 else:
13 # remember the name of the file that this diff affects
14 m = re.match(r'--- [^/]/([^\t])', line)
15 if m: filename = m.group(1)
16 continue
17 if line.startswith('diff '):
18 header = True
19 continue
20 # hunk header - save the line number
21 m = re.match(r'@@ -(\d+),', line)
22 if m:
23 linenum = int(m.group(1))
24 continue
25 # hunk body - check for an added line with trailing whitespace
26 m = re.match(r'\+.*\s$', line)
27 if m:
28 added.append((filename, linenum))
29 if line and line[0] in ' +':
30 linenum += 1
31 return added
33 if __name__ == '__main__':
34 import os, sys
36 added = trailing_whitespace(os.popen('hg export tip'))
37 if added:
38 for filename, linenum in added:
39 print >> sys.stderr, ('%s, line %d: trailing whitespace added' %
40 (filename, linenum))
41 # save the commit message so we don't need to retype it
42 os.system('hg tip --template "{desc}" > .hg/commit.save')
43 print >> sys.stderr, 'commit message saved to .hg/commit.save'
44 sys.exit(1)