Mercurial > hgbook
annotate ja/examples/data/check_whitespace.py @ 838:d1f676a6a4b3 default tip
update mq chapter.
propagate ef53d025f410.
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Thu, 03 Dec 2009 01:26:08 +0900 |
parents | b0db5adf11c1 |
children |
rev | line source |
---|---|
44
012df94a02fe
Start hook examples. First is for trailing whitespace.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
1 #!/usr/bin/python |
012df94a02fe
Start hook examples. First is for trailing whitespace.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
2 |
49 | 3 import re |
44
012df94a02fe
Start hook examples. First is for trailing whitespace.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
4 |
49 | 5 def trailing_whitespace(difflines): |
6 added, linenum, header = [], 0, False | |
44
012df94a02fe
Start hook examples. First is for trailing whitespace.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 |
49 | 8 for line in difflines: |
9 if header: | |
65
e3894bb9d1f5
Fix bugs in check_whitespace.py.
Bryan O'Sullivan <bos@serpentine.com>
parents:
49
diff
changeset
|
10 # 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
|
11 m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line) |
e3894bb9d1f5
Fix bugs in check_whitespace.py.
Bryan O'Sullivan <bos@serpentine.com>
parents:
49
diff
changeset
|
12 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
|
13 filename = m.group(1).split('/', 1)[-1] |
49 | 14 if line.startswith('+++ '): |
15 header = False | |
16 continue | |
17 if line.startswith('diff '): | |
18 header = True | |
19 continue | |
20 # hunk header - save the line number | |
65
e3894bb9d1f5
Fix bugs in check_whitespace.py.
Bryan O'Sullivan <bos@serpentine.com>
parents:
49
diff
changeset
|
21 m = re.match(r'@@ -\d+,\d+ \+(\d+),', line) |
49 | 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 | |
44
012df94a02fe
Start hook examples. First is for trailing whitespace.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
32 |
49 | 33 if __name__ == '__main__': |
34 import os, sys | |
35 | |
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) |