hooks: Add a hook to limit the size of any individual file
authorNathan Binkert <nate@binkert.org>
Fri, 6 Jan 2012 23:19:13 +0000 (18:19 -0500)
committerNathan Binkert <nate@binkert.org>
Fri, 6 Jan 2012 23:19:13 +0000 (18:19 -0500)
util/hgfilesize.py [new file with mode: 0644]

diff --git a/util/hgfilesize.py b/util/hgfilesize.py
new file mode 100644 (file)
index 0000000..8b9ad67
--- /dev/null
@@ -0,0 +1,32 @@
+from mercurial import context
+from mercurial.i18n import _
+
+'''
+[extensions]
+hgfilesize=~/m5/incoming/util/hgfilesize.py
+
+[hooks]
+pretxncommit = python:hgfilesize.limit_file_size
+pretxnchangegroup = python:hgfilesize.limit_file_size
+
+[limit_file_size]
+maximum_file_size = 200000
+'''
+
+def limit_file_size(ui, repo, node=None, **kwargs):
+    '''forbid files over a given size'''
+
+    # default limit is 1 MB
+    limit = int(ui.config('limit_file_size', 'maximum_file_size', 1024*1024))
+    existing_tip = context.changectx(repo, node).rev()
+    new_tip = context.changectx(repo, 'tip').rev()
+    for rev in xrange(existing_tip, new_tip + 1):
+        ctx = context.changectx(repo, rev)
+        for f in ctx.files():
+            fctx = ctx.filectx(f)
+            if fctx.size() > limit:
+                ui.write(_('file %s of %s is too large: %d > %d\n') % \
+                             (f, ctx, fctx.size(), limit))
+                return True # This is invalid
+
+    return False # Things are OK.