質問

したいと思っていますMercurialフックを実行する深いる取引を中止の取引の場合はバイナリファイルをすることにより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!

私は知らない書Mercurial用フックさんのかばよいのかわからず戸惑います。なぜ、フックがファイルが削除された場合hg既に知っているのでしょうか?があるので、この問題を修正するにはフックが動作するようにすべての時間がかかる?

更新(解): 私を変更したフックのフィルター行ファイルの紫外線を受けることにより、チェンジセットから表示します。

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 = ...を削除することができます)。

あなたは現代のHGを使用している場合は、代わりにctx = context.changectx(repo, node)が、ctx = repo[node]をしない。

他のヒント

これは、最近の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

ここで何が起こっているの?まず、(HG 1.9の「HGヘルプファイルセット」を参照)問題となっているすべての変更されたファイルを見つけるためのファイルセットを持っています。 「見つけ」コマンドは、それだけでリストファイルを除いて、状態のようなもので、それが何かを見つけた場合は0を返します。そして、我々はコミット保留中を見て「-r先端」を指定します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top