hgbook

view en/examples/data/check_whitespace.py @ 44:012df94a02fe

Start hook examples. First is for trailing whitespace.
author Bryan O'Sullivan <bos@serpentine.com>
date Sun Jul 23 23:25:52 2006 -0700 (2006-07-23)
parents
children 18210d46491f
line source
1 #!/usr/bin/python
3 import os, re, sys
5 count = 0
7 for line in os.popen('hg export tip'):
8 # remember the name of the file that this diff affects
9 m = re.match(r'^--- [^/]/([^\t])', line)
10 if m:
11 filename = m.group(1)
12 continue
13 # remember the line number
14 m = re.match(r'^@@ -(\d+),')
15 if m:
16 linenum = m.group(1)
17 continue
18 linenum += 1
19 # check for an added line with trailing whitespace
20 m = re.match(r'^\+.*\s$', line)
21 if m:
22 print >> sys.stderr, ('%s:%d: trailing whitespace introduced' %
23 (filename, linenum))
24 count += 1
26 if count:
27 # save the commit message so we don't need to retype it
28 os.system('hg tip --template "{desc}" > .hg/commit.save')
29 print >> sys.stderr, 'commit message saved to .hg/commit.save'
31 sys.exit(count)