hgbook

diff en/examples/data/check_whitespace.py @ 49:18210d46491f

More hook content.
author Bryan O'Sullivan <bos@serpentine.com>
date Tue Jul 25 00:18:31 2006 -0700 (2006-07-25)
parents 012df94a02fe
children e3894bb9d1f5
line diff
     1.1 --- a/en/examples/data/check_whitespace.py	Sun Jul 23 23:25:52 2006 -0700
     1.2 +++ b/en/examples/data/check_whitespace.py	Tue Jul 25 00:18:31 2006 -0700
     1.3 @@ -1,31 +1,44 @@
     1.4  #!/usr/bin/python
     1.5  
     1.6 -import os, re, sys
     1.7 +import re
     1.8  
     1.9 -count = 0
    1.10 +def trailing_whitespace(difflines):
    1.11 +    added, linenum, header = [], 0, False
    1.12  
    1.13 -for line in os.popen('hg export tip'):
    1.14 -    # remember the name of the file that this diff affects
    1.15 -    m = re.match(r'^--- [^/]/([^\t])', line)
    1.16 -    if m: 
    1.17 -	filename = m.group(1)
    1.18 -	continue
    1.19 -    # remember the line number
    1.20 -    m = re.match(r'^@@ -(\d+),')
    1.21 -    if m:
    1.22 -        linenum = m.group(1)
    1.23 -        continue
    1.24 -    linenum += 1
    1.25 -    # check for an added line with trailing whitespace
    1.26 -    m = re.match(r'^\+.*\s$', line)
    1.27 -    if m:
    1.28 -	print >> sys.stderr, ('%s:%d: trailing whitespace introduced' %
    1.29 -                              (filename, linenum))
    1.30 -        count += 1
    1.31 +    for line in difflines:
    1.32 +        if header:
    1.33 +            if line.startswith('+++ '):
    1.34 +                header = False
    1.35 +            else:
    1.36 +                # remember the name of the file that this diff affects
    1.37 +                m = re.match(r'--- [^/]/([^\t])', line)
    1.38 +                if m: filename = m.group(1)
    1.39 +            continue
    1.40 +        if line.startswith('diff '):
    1.41 +            header = True
    1.42 +            continue
    1.43 +        # hunk header - save the line number
    1.44 +        m = re.match(r'@@ -(\d+),', line)
    1.45 +        if m:
    1.46 +            linenum = int(m.group(1))
    1.47 +            continue
    1.48 +        # hunk body - check for an added line with trailing whitespace
    1.49 +        m = re.match(r'\+.*\s$', line)
    1.50 +        if m:
    1.51 +            added.append((filename, linenum))
    1.52 +        if line and line[0] in ' +':
    1.53 +            linenum += 1
    1.54 +    return added
    1.55  
    1.56 -if count:
    1.57 -    # save the commit message so we don't need to retype it
    1.58 -    os.system('hg tip --template "{desc}" > .hg/commit.save')
    1.59 -    print >> sys.stderr, 'commit message saved to .hg/commit.save'
    1.60 -
    1.61 -sys.exit(count)
    1.62 +if __name__ == '__main__':
    1.63 +    import os, sys
    1.64 +    
    1.65 +    added = trailing_whitespace(os.popen('hg export tip'))
    1.66 +    if added:
    1.67 +        for filename, linenum in added:
    1.68 +            print >> sys.stderr, ('%s, line %d: trailing whitespace added' %
    1.69 +                                  (filename, linenum))
    1.70 +        # save the commit message so we don't need to retype it
    1.71 +        os.system('hg tip --template "{desc}" > .hg/commit.save')
    1.72 +        print >> sys.stderr, 'commit message saved to .hg/commit.save'
    1.73 +        sys.exit(1)