# HG changeset patch # User Sjoerd Mullender <sjoerd@acm.org> # Date 1368476354 -7200 # Node ID 561cb4f73b83d2d8a1b90c65d5b82014a8249751 # Parent adc4e99b4a4ad72c31e50ff5bc84c86b41be12b9 Turn into proper hook function (with backward compatibility). diff -r adc4e99b4a4a -r 561cb4f73b83 check_whitespace.py --- 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)