水银勾禁止投入大量的二进制文件
-
23-09-2019 - |
题
我想有一个水银挂钩,将提交如果二进制文件被提交超过1兆字节大于将中止交易的交易之前运行。我发现下面的代码工作除了一个问题很好。如果我的变更包括删除文件,这个钩子会抛出异常。
钩(我使用pretxncommit = python:checksize.newbinsize
):
from mercurial import context, util
from mercurial.i18n import _
import mercurial.node as dpynode
'''hooks to forbid adding binary file over a given size
Ensure the PYTHONPATH is pointing where hg_checksize.py is and setup your
repo .hg/hgrc like this:
[hooks]
pretxncommit = python:checksize.newbinsize
pretxnchangegroup = python:checksize.newbinsize
preoutgoing = python:checksize.nopull
[limits]
maxnewbinsize = 10240
'''
def newbinsize(ui, repo, node=None, **kwargs):
'''forbid to add binary files over a given size'''
forbid = False
# default limit is 10 MB
limit = int(ui.config('limits', 'maxnewbinsize', 10000000))
tip = context.changectx(repo, 'tip').rev()
ctx = context.changectx(repo, node)
for rev in range(ctx.rev(), tip+1):
ctx = context.changectx(repo, rev)
print ctx.files()
for f in ctx.files():
fctx = ctx.filectx(f)
filecontent = fctx.data()
# check only for new files
if not fctx.parents():
if len(filecontent) > limit and util.binary(filecontent):
msg = 'new binary file %s of %s is too large: %ld > %ld\n'
hname = dpynode.short(ctx.node())
ui.write(_(msg) % (f, hname, len(filecontent), limit))
forbid = True
return forbid
在例外:
$ hg commit -m 'commit message'
error: pretxncommit hook raised an exception: apps/helpers/templatetags/include_extends.py@bced6272d8f4: not found in manifest
transaction abort!
rollback completed
abort: apps/helpers/templatetags/include_extends.py@bced6272d8f4: not found in manifest!
我不熟悉编写水银挂钩,所以我很困惑的是怎么回事。为什么钩照顾被删除一个文件,如果汞已经知道了吗?有没有办法解决这个挂钩上,使得它的工作原理所有的时间?
<强>更新(解决):强> 我修改了钩来过滤掉变更删除的文件。
def newbinsize(ui, repo, node=None, **kwargs):
'''forbid to add binary files over a given size'''
forbid = False
# default limit is 10 MB
limit = int(ui.config('limits', 'maxnewbinsize', 10000000))
ctx = repo[node]
for rev in xrange(ctx.rev(), len(repo)):
ctx = context.changectx(repo, rev)
# do not check the size of files that have been removed
# files that have been removed do not have filecontexts
# to test for whether a file was removed, test for the existence of a filecontext
filecontexts = list(ctx)
def file_was_removed(f):
"""Returns True if the file was removed"""
if f not in filecontexts:
return True
else:
return False
for f in itertools.ifilterfalse(file_was_removed, ctx.files()):
fctx = ctx.filectx(f)
filecontent = fctx.data()
# check only for new files
if not fctx.parents():
if len(filecontent) > limit and util.binary(filecontent):
msg = 'new binary file %s of %s is too large: %ld > %ld\n'
hname = dpynode.short(ctx.node())
ui.write(_(msg) % (f, hname, len(filecontent), limit))
forbid = True
return forbid
解决方案
for f in ctx.files()
将包括删除的文件,就需要过滤出那些
(和可以取代通过for rev in range(ctx.rev(), tip+1):
for rev in xrange(ctx.rev(), len(repo)):
,和删除tip = ...
)
如果您使用的是现代的汞,你不这样做,但ctx = context.changectx(repo, node)
代替ctx = repo[node]
其他提示
这是很容易在一个壳钩做在最近水银:
if hg locate -r tip "set:(added() or modified()) and binary() and size('>100k')"; then
echo "bad files!"
exit 1
else
exit 0
fi
这是怎么回事吗?首先,我们有一个文件集中找到的所有更改的文件有问题的(参见“汞的帮助文件集”中的汞1.9)。在“查找”命令般的地位,但它只是列表中的文件,如果发现任何会返回0。我们指定“-r尖端”来看看未决提交。
不隶属于 StackOverflow