annotate en/examples/ch09/check_whitespace.py.lst @ 821:88828b784971

Add more complex example hook
author Bryan O'Sullivan <bos@serpentine.com>
date Tue, 28 Apr 2009 23:10:43 -0700
parents en/examples/data/check_whitespace.py@e3894bb9d1f5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
821
88828b784971 Add more complex example hook
Bryan O'Sullivan <bos@serpentine.com>
parents: 65
diff changeset
1 #!/usr/bin/env python
88828b784971 Add more complex example hook
Bryan O'Sullivan <bos@serpentine.com>
parents: 65
diff changeset
2 #
88828b784971 Add more complex example hook
Bryan O'Sullivan <bos@serpentine.com>
parents: 65
diff changeset
3 # save as .hg/check_whitespace.py and make executable
44
012df94a02fe Start hook examples. First is for trailing whitespace.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
4
49
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
5 import re
44
012df94a02fe Start hook examples. First is for trailing whitespace.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
6
49
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
7 def trailing_whitespace(difflines):
821
88828b784971 Add more complex example hook
Bryan O'Sullivan <bos@serpentine.com>
parents: 65
diff changeset
8 #
88828b784971 Add more complex example hook
Bryan O'Sullivan <bos@serpentine.com>
parents: 65
diff changeset
9 linenum, header = 0, False
44
012df94a02fe Start hook examples. First is for trailing whitespace.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
10
49
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
11 for line in difflines:
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
12 if header:
65
e3894bb9d1f5 Fix bugs in check_whitespace.py.
Bryan O'Sullivan <bos@serpentine.com>
parents: 49
diff changeset
13 # remember the name of the file that this diff affects
e3894bb9d1f5 Fix bugs in check_whitespace.py.
Bryan O'Sullivan <bos@serpentine.com>
parents: 49
diff changeset
14 m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line)
e3894bb9d1f5 Fix bugs in check_whitespace.py.
Bryan O'Sullivan <bos@serpentine.com>
parents: 49
diff changeset
15 if m and m.group(1) != '/dev/null':
e3894bb9d1f5 Fix bugs in check_whitespace.py.
Bryan O'Sullivan <bos@serpentine.com>
parents: 49
diff changeset
16 filename = m.group(1).split('/', 1)[-1]
49
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
17 if line.startswith('+++ '):
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
18 header = False
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
19 continue
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
20 if line.startswith('diff '):
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
21 header = True
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
22 continue
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
23 # hunk header - save the line number
65
e3894bb9d1f5 Fix bugs in check_whitespace.py.
Bryan O'Sullivan <bos@serpentine.com>
parents: 49
diff changeset
24 m = re.match(r'@@ -\d+,\d+ \+(\d+),', line)
49
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
25 if m:
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
26 linenum = int(m.group(1))
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
27 continue
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
28 # hunk body - check for an added line with trailing whitespace
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
29 m = re.match(r'\+.*\s$', line)
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
30 if m:
821
88828b784971 Add more complex example hook
Bryan O'Sullivan <bos@serpentine.com>
parents: 65
diff changeset
31 yield filename, linenum
49
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
32 if line and line[0] in ' +':
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
33 linenum += 1
44
012df94a02fe Start hook examples. First is for trailing whitespace.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
34
49
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
35 if __name__ == '__main__':
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
36 import os, sys
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
37
821
88828b784971 Add more complex example hook
Bryan O'Sullivan <bos@serpentine.com>
parents: 65
diff changeset
38 added = 0
88828b784971 Add more complex example hook
Bryan O'Sullivan <bos@serpentine.com>
parents: 65
diff changeset
39 for filename, linenum in trailing_whitespace(os.popen('hg export tip')):
88828b784971 Add more complex example hook
Bryan O'Sullivan <bos@serpentine.com>
parents: 65
diff changeset
40 print >> sys.stderr, ('%s, line %d: trailing whitespace added' %
88828b784971 Add more complex example hook
Bryan O'Sullivan <bos@serpentine.com>
parents: 65
diff changeset
41 (filename, linenum))
88828b784971 Add more complex example hook
Bryan O'Sullivan <bos@serpentine.com>
parents: 65
diff changeset
42 added += 1
49
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
43 if added:
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
44 # save the commit message so we don't need to retype it
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
45 os.system('hg tip --template "{desc}" > .hg/commit.save')
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
46 print >> sys.stderr, 'commit message saved to .hg/commit.save'
18210d46491f More hook content.
Bryan O'Sullivan <bos@serpentine.com>
parents: 44
diff changeset
47 sys.exit(1)