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