# HG changeset patch # User Bryan O'Sullivan # Date 1240985443 25200 # Node ID 88828b784971319ab9778090fbf01f4d3052f2c5 # Parent 3edacbff2b43a1d8d1b58141badb520e9c0eb2ab Add more complex example hook diff -r 3edacbff2b43 -r 88828b784971 en/Makefile --- a/en/Makefile Tue Apr 28 22:49:50 2009 -0700 +++ b/en/Makefile Tue Apr 28 23:10:43 2009 -0700 @@ -35,7 +35,6 @@ filenames \ hook.msglen \ hook.simple \ - hook.ws \ issue29 \ mq.guards \ mq.qinit-help \ diff -r 3edacbff2b43 -r 88828b784971 en/ch09-hook.xml --- a/en/ch09-hook.xml Tue Apr 28 22:49:50 2009 -0700 +++ b/en/ch09-hook.xml Tue Apr 28 23:10:43 2009 -0700 @@ -556,7 +556,7 @@ role="hg-cmd">hg commit again. -&interaction.hook.ws.simple; + &interaction.ch09-hook.ws.simple; In this example, we introduce a simple pretxncommit hook that checks for @@ -569,6 +569,8 @@ trailing whitespace cause problems. + &ch09-check_whitespace.py.lst; + The above version is much more complex, but also more useful. It parses a unified diff to see if any lines add trailing whitespace, and prints the name of the file and the @@ -581,7 +583,7 @@ the saved commit message once you've corrected the problem. -&interaction.hook.ws.better; + &interaction.ch09-hook.ws.better; As a final aside, note in the example above the use of perl's in-place editing feature to get rid diff -r 3edacbff2b43 -r 88828b784971 en/examples/auto-snippets.xml --- a/en/examples/auto-snippets.xml Tue Apr 28 22:49:50 2009 -0700 +++ b/en/examples/auto-snippets.xml Tue Apr 28 23:10:43 2009 -0700 @@ -1,4 +1,5 @@ + @@ -65,6 +66,8 @@ + + @@ -122,8 +125,6 @@ - - diff -r 3edacbff2b43 -r 88828b784971 en/examples/ch09/check_whitespace.py.lst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/en/examples/ch09/check_whitespace.py.lst Tue Apr 28 23:10:43 2009 -0700 @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# +# save as .hg/check_whitespace.py and make executable + +import re + +def trailing_whitespace(difflines): + # + linenum, header = 0, False + + for line in difflines: + if header: + # remember the name of the file that this diff affects + m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line) + if m and m.group(1) != '/dev/null': + filename = m.group(1).split('/', 1)[-1] + if line.startswith('+++ '): + header = False + continue + if line.startswith('diff '): + header = True + continue + # hunk header - save the line number + m = re.match(r'@@ -\d+,\d+ \+(\d+),', line) + if m: + linenum = int(m.group(1)) + continue + # hunk body - check for an added line with trailing whitespace + m = re.match(r'\+.*\s$', line) + if m: + yield filename, linenum + if line and line[0] in ' +': + linenum += 1 + +if __name__ == '__main__': + import os, sys + + added = 0 + for filename, linenum in trailing_whitespace(os.popen('hg export tip')): + print >> sys.stderr, ('%s, line %d: trailing whitespace added' % + (filename, linenum)) + added += 1 + if added: + # save the commit message so we don't need to retype it + os.system('hg tip --template "{desc}" > .hg/commit.save') + print >> sys.stderr, 'commit message saved to .hg/commit.save' + sys.exit(1) diff -r 3edacbff2b43 -r 88828b784971 en/examples/ch09/hook.ws --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/en/examples/ch09/hook.ws Tue Apr 28 23:10:43 2009 -0700 @@ -0,0 +1,32 @@ +#!/bin/bash + +hg init a +cd a +echo '[hooks]' > .hg/hgrc +echo "pretxncommit.whitespace = hg export tip | (! egrep -q '^\\+.*[ \\t]$')" >> .hg/hgrc + +#$ name: simple + +cat .hg/hgrc +echo 'a ' > a +hg commit -A -m 'test with trailing whitespace' +echo 'a' > a +hg commit -A -m 'drop trailing whitespace and try again' + +#$ name: + +echo '[hooks]' > .hg/hgrc +echo "pretxncommit.whitespace = .hg/check_whitespace.py" >> .hg/hgrc +cp $EXAMPLE_DIR/ch09/check_whitespace.py.lst .hg/check_whitespace.py +chmod +x .hg/check_whitespace.py + +#$ name: better + +cat .hg/hgrc +echo 'a ' >> a +hg commit -A -m 'add new line with trailing whitespace' +sed -i 's, *$,,' a +hg commit -A -m 'trimmed trailing whitespace' + +#$ name: +exit 0 diff -r 3edacbff2b43 -r 88828b784971 en/examples/data/check_whitespace.py --- a/en/examples/data/check_whitespace.py Tue Apr 28 22:49:50 2009 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -#!/usr/bin/python - -import re - -def trailing_whitespace(difflines): - added, linenum, header = [], 0, False - - for line in difflines: - if header: - # remember the name of the file that this diff affects - m = re.match(r'(?:---|\+\+\+) ([^\t]+)', line) - if m and m.group(1) != '/dev/null': - filename = m.group(1).split('/', 1)[-1] - if line.startswith('+++ '): - header = False - continue - if line.startswith('diff '): - header = True - continue - # hunk header - save the line number - m = re.match(r'@@ -\d+,\d+ \+(\d+),', line) - if m: - linenum = int(m.group(1)) - continue - # hunk body - check for an added line with trailing whitespace - m = re.match(r'\+.*\s$', line) - if m: - added.append((filename, linenum)) - if line and line[0] in ' +': - linenum += 1 - return added - -if __name__ == '__main__': - import os, sys - - added = trailing_whitespace(os.popen('hg export tip')) - if added: - for filename, linenum in added: - print >> sys.stderr, ('%s, line %d: trailing whitespace added' % - (filename, linenum)) - # save the commit message so we don't need to retype it - os.system('hg tip --template "{desc}" > .hg/commit.save') - print >> sys.stderr, 'commit message saved to .hg/commit.save' - sys.exit(1) diff -r 3edacbff2b43 -r 88828b784971 en/examples/hook.ws --- a/en/examples/hook.ws Tue Apr 28 22:49:50 2009 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -#!/bin/bash - -hg init a -cd a -echo '[hooks]' > .hg/hgrc -echo "pretxncommit.whitespace = hg export tip | (! egrep -q '^\\+.*[ \\t]$')" >> .hg/hgrc - -#$ name: simple - -cat .hg/hgrc -echo 'a ' > a -hg commit -A -m 'test with trailing whitespace' -echo 'a' > a -hg commit -A -m 'drop trailing whitespace and try again' - -#$ name: - -echo '[hooks]' > .hg/hgrc -echo "pretxncommit.whitespace = .hg/check_whitespace.py" >> .hg/hgrc -cp $EXAMPLE_DIR/data/check_whitespace.py .hg - -#$ name: better - -cat .hg/hgrc -echo 'a ' >> a -hg commit -A -m 'add new line with trailing whitespace' -sed -i 's, *$,,' a -hg commit -A -m 'trimmed trailing whitespace' - -#$ name: -exit 0