changeset 821:88828b784971

Add more complex example hook
author Bryan O'Sullivan <bos@serpentine.com>
date Tue, 28 Apr 2009 23:10:43 -0700
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
diffstat 7 files changed, 86 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- 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 \
--- 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</command> again.
       </para>
 
-&interaction.hook.ws.simple;
+      &interaction.ch09-hook.ws.simple;
 
       <para id="x_235">In this example, we introduce a simple <literal
 	  role="hook">pretxncommit</literal> hook that checks for
@@ -569,6 +569,8 @@
 	trailing whitespace cause problems.
       </para>
 
+      &ch09-check_whitespace.py.lst;
+
       <para id="x_236">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.
       </para>
 
-&interaction.hook.ws.better;
+      &interaction.ch09-hook.ws.better;
 
       <para id="x_237">As a final aside, note in the example above the use of
 	<command>perl</command>'s in-place editing feature to get rid
--- 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 @@
 <!ENTITY ch06-apache-config.lst SYSTEM "results/ch06-apache-config.lst.lxo">
+<!ENTITY ch09-check_whitespace.py.lst SYSTEM "results/ch09-check_whitespace.py.lst.lxo">
 <!ENTITY ch10-bugzilla-config.lst SYSTEM "results/ch10-bugzilla-config.lst.lxo">
 <!ENTITY ch10-notify-config-mail.lst SYSTEM "results/ch10-notify-config-mail.lst.lxo">
 <!ENTITY ch10-notify-config.lst SYSTEM "results/ch10-notify-config.lst.lxo">
@@ -65,6 +66,8 @@
 <!ENTITY interaction.ch04-resolve.merge SYSTEM "results/ch04-resolve.merge.lxo">
 <!ENTITY interaction.ch04-resolve.pull SYSTEM "results/ch04-resolve.pull.lxo">
 <!ENTITY interaction.ch04-resolve.right SYSTEM "results/ch04-resolve.right.lxo">
+<!ENTITY interaction.ch09-hook.ws.better SYSTEM "results/ch09-hook.ws.better.lxo">
+<!ENTITY interaction.ch09-hook.ws.simple SYSTEM "results/ch09-hook.ws.simple.lxo">
 <!ENTITY interaction.ch11-qdelete.convert SYSTEM "results/ch11-qdelete.convert.lxo">
 <!ENTITY interaction.ch11-qdelete.go SYSTEM "results/ch11-qdelete.go.lxo">
 <!ENTITY interaction.ch11-qdelete.import SYSTEM "results/ch11-qdelete.import.lxo">
@@ -122,8 +125,6 @@
 <!ENTITY interaction.hook.simple.ext SYSTEM "results/hook.simple.ext.lxo">
 <!ENTITY interaction.hook.simple.init SYSTEM "results/hook.simple.init.lxo">
 <!ENTITY interaction.hook.simple.pretxncommit SYSTEM "results/hook.simple.pretxncommit.lxo">
-<!ENTITY interaction.hook.ws.better SYSTEM "results/hook.ws.better.lxo">
-<!ENTITY interaction.hook.ws.simple SYSTEM "results/hook.ws.simple.lxo">
 <!ENTITY interaction.issue29.go SYSTEM "results/issue29.go.lxo">
 <!ENTITY interaction.mq.dodiff.diff SYSTEM "results/mq.dodiff.diff.lxo">
 <!ENTITY interaction.mq.guards.init SYSTEM "results/mq.guards.init.lxo">
--- /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)
--- /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
--- 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)
--- 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