hgbook

diff en/examples/data/check_whitespace.py @ 44:012df94a02fe

Start hook examples. First is for trailing whitespace.
author Bryan O'Sullivan <bos@serpentine.com>
date Sun Jul 23 23:25:52 2006 -0700 (2006-07-23)
parents
children 18210d46491f
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/en/examples/data/check_whitespace.py	Sun Jul 23 23:25:52 2006 -0700
     1.3 @@ -0,0 +1,31 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +import os, re, sys
     1.7 +
     1.8 +count = 0
     1.9 +
    1.10 +for line in os.popen('hg export tip'):
    1.11 +    # remember the name of the file that this diff affects
    1.12 +    m = re.match(r'^--- [^/]/([^\t])', line)
    1.13 +    if m: 
    1.14 +	filename = m.group(1)
    1.15 +	continue
    1.16 +    # remember the line number
    1.17 +    m = re.match(r'^@@ -(\d+),')
    1.18 +    if m:
    1.19 +        linenum = m.group(1)
    1.20 +        continue
    1.21 +    linenum += 1
    1.22 +    # check for an added line with trailing whitespace
    1.23 +    m = re.match(r'^\+.*\s$', line)
    1.24 +    if m:
    1.25 +	print >> sys.stderr, ('%s:%d: trailing whitespace introduced' %
    1.26 +                              (filename, linenum))
    1.27 +        count += 1
    1.28 +
    1.29 +if count:
    1.30 +    # save the commit message so we don't need to retype it
    1.31 +    os.system('hg tip --template "{desc}" > .hg/commit.save')
    1.32 +    print >> sys.stderr, 'commit message saved to .hg/commit.save'
    1.33 +
    1.34 +sys.exit(count)