hgbook

annotate es/examples/data/check_whitespace.py @ 649:d13c7c706a58

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