1from mercurial import context
2from mercurial.i18n import _
3
4'''
5[extensions]
6hgfilesize=~/m5/incoming/util/hgfilesize.py
7
8[hooks]
9pretxncommit = python:hgfilesize.limit_file_size
10pretxnchangegroup = python:hgfilesize.limit_file_size
11
12[limit_file_size]
13maximum_file_size = 200000
14'''
15
16def limit_file_size(ui, repo, node=None, **kwargs):
17    '''forbid files over a given size'''
18
19    # default limit is 1 MB
20    limit = int(ui.config('limit_file_size', 'maximum_file_size', 1024*1024))
21    existing_tip = context.changectx(repo, node).rev()
22    new_tip = context.changectx(repo, 'tip').rev()
23    for rev in xrange(existing_tip, new_tip + 1):
24        ctx = context.changectx(repo, rev)
25        for f in ctx.files():
26            if f not in ctx:
27                continue
28            fctx = ctx.filectx(f)
29            if fctx.size() > limit:
30                ui.write(_('file %s of %s is too large: %d > %d\n') % \
31                             (f, ctx, fctx.size(), limit))
32                return True # This is invalid
33
34    return False # Things are OK.
35