Commits

Bryan O'Sullivan committed 88828b7

Add more complex example hook

Comments (0)

Files changed (7)

 	filenames \
 	hook.msglen \
 	hook.simple \
-	hook.ws \
 	issue29 \
 	mq.guards \
 	mq.qinit-help \
 	  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
 	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
 	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

en/examples/auto-snippets.xml

 <!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">
 <!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">
 <!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">

en/examples/ch09/check_whitespace.py.lst

+#!/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)

en/examples/ch09/hook.ws

+#!/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

en/examples/data/check_whitespace.py

-#!/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)

en/examples/hook.ws

-#!/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