I want to have a Mercurial hook that will run before committing a transaction that will abort the transaction if a binary file being committed is greater than 1 megabyte. I
This is really easy to do in a shell hook in recent Mercurial:
if hg locate -r tip "set:(added() or modified()) and binary() and size('>100k')"; then
echo "bad files!"
exit 1
else
exit 0
fi
What's going on here? First we have a fileset to find all the changed files that are problematic (see 'hg help filesets' in hg 1.9). The 'locate' command is like status, except it just lists files and returns 0 if it finds anything. And we specify '-r tip' to look at the pending commit.
for f in ctx.files()
will include removed files, you need to filter those out.
(and you can replace for rev in range(ctx.rev(), tip+1):
by for rev in xrange(ctx.rev(), len(repo)):
, and remove tip = ...
)
If you're using a modern hg, you don't do ctx = context.changectx(repo, node)
but ctx = repo[node]
instead.