changeset 3:561cb4f73b83

Turn into proper hook function (with backward compatibility).
author Sjoerd Mullender <sjoerd@acm.org>
date Mon, 13 May 2013 22:19:14 +0200 (2013-05-13)
parents adc4e99b4a4a
children 8884ce4b78d4
files check_whitespace.py
diffstat 1 files changed, 15 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/check_whitespace.py	Mon Apr 02 22:28:07 2012 +0200
+++ b/check_whitespace.py	Mon May 13 22:19:14 2013 +0200
@@ -6,6 +6,16 @@
 # This script is based on the code in the Mercurial book
 # <http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html#id403945>
 
+'''hook to refuse commits that introduce some bad inputs.
+Inputs that are refused are conflict markers (rows of < or >
+characters), and tabs and trailing white space in Python sources.
+
+Usage: in your ~/.hgrc file add:
+[hooks]
+pretxncommit.whitespace = python:/path/to/check_whitespace.py:hook
+pretxnchangegroup.whitespace = python:/path/to/check_whitespace.py:hook
+'''
+
 import re
 
 def trailing_whitespace(difflines):
@@ -54,7 +64,7 @@
         if line and line[0] in ' +':
             linenum += 1
 
-if __name__ == '__main__':
+def hook(ui, repo, hooktype, node, **kwargs):
     import os, sys, subprocess
 
     added = 0
@@ -68,4 +78,8 @@
         subprocess.call(['hg', 'tip', '--template', '{desc}'],
                         stdout=open(cmtsv, 'w'))
         print >> sys.stderr, 'commit message saved to %s' % cmtsv
+        return True
+
+if __name__ == '__main__':
+    if hook(None, None, None, None):
         sys.exit(1)