hgbook
annotate ja/examples/data/check_whitespace.py @ 1078:708f570346a9
2.4.1 some para zh translated
author | Zhaoping Sun <zhaopingsun@gmail.com> |
---|---|
date | Wed Nov 18 22:22:34 2009 -0500 (2009-11-18) |
parents | |
children |
rev | line source |
---|---|
foozy@708 | 1 #!/usr/bin/python |
foozy@708 | 2 |
foozy@708 | 3 import re |
foozy@708 | 4 |
foozy@708 | 5 def trailing_whitespace(difflines): |
foozy@708 | 6 added, linenum, header = [], 0, False |
foozy@708 | 7 |
foozy@708 | 8 for line in difflines: |
foozy@708 | 9 if header: |
foozy@708 | 10 # remember the name of the file that this diff affects |
foozy@708 | 11 m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line) |
foozy@708 | 12 if m and m.group(1) != '/dev/null': |
foozy@708 | 13 filename = m.group(1).split('/', 1)[-1] |
foozy@708 | 14 if line.startswith('+++ '): |
foozy@708 | 15 header = False |
foozy@708 | 16 continue |
foozy@708 | 17 if line.startswith('diff '): |
foozy@708 | 18 header = True |
foozy@708 | 19 continue |
foozy@708 | 20 # hunk header - save the line number |
foozy@708 | 21 m = re.match(r'@@ -\d+,\d+ \+(\d+),', line) |
foozy@708 | 22 if m: |
foozy@708 | 23 linenum = int(m.group(1)) |
foozy@708 | 24 continue |
foozy@708 | 25 # hunk body - check for an added line with trailing whitespace |
foozy@708 | 26 m = re.match(r'\+.*\s$', line) |
foozy@708 | 27 if m: |
foozy@708 | 28 added.append((filename, linenum)) |
foozy@708 | 29 if line and line[0] in ' +': |
foozy@708 | 30 linenum += 1 |
foozy@708 | 31 return added |
foozy@708 | 32 |
foozy@708 | 33 if __name__ == '__main__': |
foozy@708 | 34 import os, sys |
foozy@708 | 35 |
foozy@708 | 36 added = trailing_whitespace(os.popen('hg export tip')) |
foozy@708 | 37 if added: |
foozy@708 | 38 for filename, linenum in added: |
foozy@708 | 39 print >> sys.stderr, ('%s, line %d: trailing whitespace added' % |
foozy@708 | 40 (filename, linenum)) |
foozy@708 | 41 # save the commit message so we don't need to retype it |
foozy@708 | 42 os.system('hg tip --template "{desc}" > .hg/commit.save') |
foozy@708 | 43 print >> sys.stderr, 'commit message saved to .hg/commit.save' |
foozy@708 | 44 sys.exit(1) |