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@65
|
10 # remember the name of the file that this diff affects
|
bos@65
|
11 m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line)
|
bos@65
|
12 if m and m.group(1) != '/dev/null':
|
bos@65
|
13 filename = m.group(1).split('/', 1)[-1]
|
bos@49
|
14 if line.startswith('+++ '):
|
bos@49
|
15 header = False
|
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@65
|
21 m = re.match(r'@@ -\d+,\d+ \+(\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)
|