hgbook

changeset 694:88828b784971

Add more complex example hook
author Bryan O'Sullivan <bos@serpentine.com>
date Tue Apr 28 23:10:43 2009 -0700 (2009-04-28)
parents 3edacbff2b43
children fd2e83ffb165
files en/Makefile en/ch09-hook.xml en/examples/auto-snippets.xml en/examples/ch09/check_whitespace.py.lst en/examples/ch09/hook.ws en/examples/data/check_whitespace.py en/examples/hook.ws
line diff
     1.1 --- a/en/Makefile	Tue Apr 28 22:49:50 2009 -0700
     1.2 +++ b/en/Makefile	Tue Apr 28 23:10:43 2009 -0700
     1.3 @@ -35,7 +35,6 @@
     1.4  	filenames \
     1.5  	hook.msglen \
     1.6  	hook.simple \
     1.7 -	hook.ws \
     1.8  	issue29 \
     1.9  	mq.guards \
    1.10  	mq.qinit-help \
     2.1 --- a/en/ch09-hook.xml	Tue Apr 28 22:49:50 2009 -0700
     2.2 +++ b/en/ch09-hook.xml	Tue Apr 28 23:10:43 2009 -0700
     2.3 @@ -556,7 +556,7 @@
     2.4  	  role="hg-cmd">hg commit</command> again.
     2.5        </para>
     2.6  
     2.7 -&interaction.hook.ws.simple;
     2.8 +      &interaction.ch09-hook.ws.simple;
     2.9  
    2.10        <para id="x_235">In this example, we introduce a simple <literal
    2.11  	  role="hook">pretxncommit</literal> hook that checks for
    2.12 @@ -569,6 +569,8 @@
    2.13  	trailing whitespace cause problems.
    2.14        </para>
    2.15  
    2.16 +      &ch09-check_whitespace.py.lst;
    2.17 +
    2.18        <para id="x_236">The above version is much more complex, but also more
    2.19  	useful.  It parses a unified diff to see if any lines add
    2.20  	trailing whitespace, and prints the name of the file and the
    2.21 @@ -581,7 +583,7 @@
    2.22  	the saved commit message once you've corrected the problem.
    2.23        </para>
    2.24  
    2.25 -&interaction.hook.ws.better;
    2.26 +      &interaction.ch09-hook.ws.better;
    2.27  
    2.28        <para id="x_237">As a final aside, note in the example above the use of
    2.29  	<command>perl</command>'s in-place editing feature to get rid
     3.1 --- a/en/examples/auto-snippets.xml	Tue Apr 28 22:49:50 2009 -0700
     3.2 +++ b/en/examples/auto-snippets.xml	Tue Apr 28 23:10:43 2009 -0700
     3.3 @@ -1,4 +1,5 @@
     3.4  <!ENTITY ch06-apache-config.lst SYSTEM "results/ch06-apache-config.lst.lxo">
     3.5 +<!ENTITY ch09-check_whitespace.py.lst SYSTEM "results/ch09-check_whitespace.py.lst.lxo">
     3.6  <!ENTITY ch10-bugzilla-config.lst SYSTEM "results/ch10-bugzilla-config.lst.lxo">
     3.7  <!ENTITY ch10-notify-config-mail.lst SYSTEM "results/ch10-notify-config-mail.lst.lxo">
     3.8  <!ENTITY ch10-notify-config.lst SYSTEM "results/ch10-notify-config.lst.lxo">
     3.9 @@ -65,6 +66,8 @@
    3.10  <!ENTITY interaction.ch04-resolve.merge SYSTEM "results/ch04-resolve.merge.lxo">
    3.11  <!ENTITY interaction.ch04-resolve.pull SYSTEM "results/ch04-resolve.pull.lxo">
    3.12  <!ENTITY interaction.ch04-resolve.right SYSTEM "results/ch04-resolve.right.lxo">
    3.13 +<!ENTITY interaction.ch09-hook.ws.better SYSTEM "results/ch09-hook.ws.better.lxo">
    3.14 +<!ENTITY interaction.ch09-hook.ws.simple SYSTEM "results/ch09-hook.ws.simple.lxo">
    3.15  <!ENTITY interaction.ch11-qdelete.convert SYSTEM "results/ch11-qdelete.convert.lxo">
    3.16  <!ENTITY interaction.ch11-qdelete.go SYSTEM "results/ch11-qdelete.go.lxo">
    3.17  <!ENTITY interaction.ch11-qdelete.import SYSTEM "results/ch11-qdelete.import.lxo">
    3.18 @@ -122,8 +125,6 @@
    3.19  <!ENTITY interaction.hook.simple.ext SYSTEM "results/hook.simple.ext.lxo">
    3.20  <!ENTITY interaction.hook.simple.init SYSTEM "results/hook.simple.init.lxo">
    3.21  <!ENTITY interaction.hook.simple.pretxncommit SYSTEM "results/hook.simple.pretxncommit.lxo">
    3.22 -<!ENTITY interaction.hook.ws.better SYSTEM "results/hook.ws.better.lxo">
    3.23 -<!ENTITY interaction.hook.ws.simple SYSTEM "results/hook.ws.simple.lxo">
    3.24  <!ENTITY interaction.issue29.go SYSTEM "results/issue29.go.lxo">
    3.25  <!ENTITY interaction.mq.dodiff.diff SYSTEM "results/mq.dodiff.diff.lxo">
    3.26  <!ENTITY interaction.mq.guards.init SYSTEM "results/mq.guards.init.lxo">
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/en/examples/ch09/check_whitespace.py.lst	Tue Apr 28 23:10:43 2009 -0700
     4.3 @@ -0,0 +1,47 @@
     4.4 +#!/usr/bin/env python
     4.5 +#
     4.6 +# save as .hg/check_whitespace.py and make executable
     4.7 +
     4.8 +import re
     4.9 +
    4.10 +def trailing_whitespace(difflines):
    4.11 +    # 
    4.12 +    linenum, header = 0, False
    4.13 +
    4.14 +    for line in difflines:
    4.15 +        if header:
    4.16 +            # remember the name of the file that this diff affects
    4.17 +            m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line)
    4.18 +            if m and m.group(1) != '/dev/null':
    4.19 +                filename = m.group(1).split('/', 1)[-1]
    4.20 +            if line.startswith('+++ '):
    4.21 +                header = False
    4.22 +            continue
    4.23 +        if line.startswith('diff '):
    4.24 +            header = True
    4.25 +            continue
    4.26 +        # hunk header - save the line number
    4.27 +        m = re.match(r'@@ -\d+,\d+ \+(\d+),', line)
    4.28 +        if m:
    4.29 +            linenum = int(m.group(1))
    4.30 +            continue
    4.31 +        # hunk body - check for an added line with trailing whitespace
    4.32 +        m = re.match(r'\+.*\s$', line)
    4.33 +        if m:
    4.34 +            yield filename, linenum
    4.35 +        if line and line[0] in ' +':
    4.36 +            linenum += 1
    4.37 +
    4.38 +if __name__ == '__main__':
    4.39 +    import os, sys
    4.40 +    
    4.41 +    added = 0
    4.42 +    for filename, linenum in trailing_whitespace(os.popen('hg export tip')):
    4.43 +        print >> sys.stderr, ('%s, line %d: trailing whitespace added' %
    4.44 +                              (filename, linenum))
    4.45 +        added += 1
    4.46 +    if added:
    4.47 +        # save the commit message so we don't need to retype it
    4.48 +        os.system('hg tip --template "{desc}" > .hg/commit.save')
    4.49 +        print >> sys.stderr, 'commit message saved to .hg/commit.save'
    4.50 +        sys.exit(1)
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/en/examples/ch09/hook.ws	Tue Apr 28 23:10:43 2009 -0700
     5.3 @@ -0,0 +1,32 @@
     5.4 +#!/bin/bash
     5.5 +
     5.6 +hg init a
     5.7 +cd a
     5.8 +echo '[hooks]' > .hg/hgrc
     5.9 +echo "pretxncommit.whitespace = hg export tip | (! egrep -q '^\\+.*[ \\t]$')" >> .hg/hgrc
    5.10 +
    5.11 +#$ name: simple
    5.12 +
    5.13 +cat .hg/hgrc
    5.14 +echo 'a ' > a
    5.15 +hg commit -A -m 'test with trailing whitespace'
    5.16 +echo 'a' > a
    5.17 +hg commit -A -m 'drop trailing whitespace and try again'
    5.18 +
    5.19 +#$ name:
    5.20 +
    5.21 +echo '[hooks]' > .hg/hgrc
    5.22 +echo "pretxncommit.whitespace = .hg/check_whitespace.py" >> .hg/hgrc
    5.23 +cp $EXAMPLE_DIR/ch09/check_whitespace.py.lst .hg/check_whitespace.py
    5.24 +chmod +x .hg/check_whitespace.py
    5.25 +
    5.26 +#$ name: better
    5.27 +
    5.28 +cat .hg/hgrc
    5.29 +echo 'a ' >> a
    5.30 +hg commit -A -m 'add new line with trailing whitespace'
    5.31 +sed -i 's, *$,,' a
    5.32 +hg commit -A -m 'trimmed trailing whitespace'
    5.33 +
    5.34 +#$ name:
    5.35 +exit 0
     6.1 --- a/en/examples/data/check_whitespace.py	Tue Apr 28 22:49:50 2009 -0700
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,44 +0,0 @@
     6.4 -#!/usr/bin/python
     6.5 -
     6.6 -import re
     6.7 -
     6.8 -def trailing_whitespace(difflines):
     6.9 -    added, linenum, header = [], 0, False
    6.10 -
    6.11 -    for line in difflines:
    6.12 -        if header:
    6.13 -            # remember the name of the file that this diff affects
    6.14 -            m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line)
    6.15 -            if m and m.group(1) != '/dev/null':
    6.16 -                filename = m.group(1).split('/', 1)[-1]
    6.17 -            if line.startswith('+++ '):
    6.18 -                header = False
    6.19 -            continue
    6.20 -        if line.startswith('diff '):
    6.21 -            header = True
    6.22 -            continue
    6.23 -        # hunk header - save the line number
    6.24 -        m = re.match(r'@@ -\d+,\d+ \+(\d+),', line)
    6.25 -        if m:
    6.26 -            linenum = int(m.group(1))
    6.27 -            continue
    6.28 -        # hunk body - check for an added line with trailing whitespace
    6.29 -        m = re.match(r'\+.*\s$', line)
    6.30 -        if m:
    6.31 -            added.append((filename, linenum))
    6.32 -        if line and line[0] in ' +':
    6.33 -            linenum += 1
    6.34 -    return added
    6.35 -
    6.36 -if __name__ == '__main__':
    6.37 -    import os, sys
    6.38 -    
    6.39 -    added = trailing_whitespace(os.popen('hg export tip'))
    6.40 -    if added:
    6.41 -        for filename, linenum in added:
    6.42 -            print >> sys.stderr, ('%s, line %d: trailing whitespace added' %
    6.43 -                                  (filename, linenum))
    6.44 -        # save the commit message so we don't need to retype it
    6.45 -        os.system('hg tip --template "{desc}" > .hg/commit.save')
    6.46 -        print >> sys.stderr, 'commit message saved to .hg/commit.save'
    6.47 -        sys.exit(1)
     7.1 --- a/en/examples/hook.ws	Tue Apr 28 22:49:50 2009 -0700
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,31 +0,0 @@
     7.4 -#!/bin/bash
     7.5 -
     7.6 -hg init a
     7.7 -cd a
     7.8 -echo '[hooks]' > .hg/hgrc
     7.9 -echo "pretxncommit.whitespace = hg export tip | (! egrep -q '^\\+.*[ \\t]$')" >> .hg/hgrc
    7.10 -
    7.11 -#$ name: simple
    7.12 -
    7.13 -cat .hg/hgrc
    7.14 -echo 'a ' > a
    7.15 -hg commit -A -m 'test with trailing whitespace'
    7.16 -echo 'a' > a
    7.17 -hg commit -A -m 'drop trailing whitespace and try again'
    7.18 -
    7.19 -#$ name:
    7.20 -
    7.21 -echo '[hooks]' > .hg/hgrc
    7.22 -echo "pretxncommit.whitespace = .hg/check_whitespace.py" >> .hg/hgrc
    7.23 -cp $EXAMPLE_DIR/data/check_whitespace.py .hg
    7.24 -
    7.25 -#$ name: better
    7.26 -
    7.27 -cat .hg/hgrc
    7.28 -echo 'a ' >> a
    7.29 -hg commit -A -m 'add new line with trailing whitespace'
    7.30 -sed -i 's, *$,,' a
    7.31 -hg commit -A -m 'trimmed trailing whitespace'
    7.32 -
    7.33 -#$ name:
    7.34 -exit 0