有没有可以防止二进制检入的git钩子 [英] Is there a git hook which can prevent binary check-ins

查看:83
本文介绍了有没有可以防止二进制检入的git钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道一个好的git钩子会检查提交时的二进制文件并抛出错误?我试图阻止二进制文件提交到我的存储库,但有时候人们会犯错。



谢谢!

我不知道现有的钩子,但git已经带有一个钩子,用于检查是否添加非ASCII名称,作为示例预先提交钩子。这可能已经存在于你的现有git仓库中,如 .git / hooks / pre-commit.sample



使用该钩子作为模板并考虑如何确定Git是否将文件处理为二进制文件还是文本?的答案?,你可以做这样的事情(参见git的半隐秘空树,其中 EMPTY_TREE ):

 #! / bin / sh 

stop_binaries = $(git config --get hooks.stop_binaries)

exec 1>& 2

if [ $ stop_binaries= true];那么
EMPTY_TREE = $(git散列对象-t树/ dev / null)
#或者:EMPTY_TREE = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
if git diff --cached --numstat $ EMPTY_TREE | grep -e'^ - '> / dev / null;然后
echo错误:提交会添加二进制文件:
git diff --cached --numstat $ EMPTY_TREE | grep -e'^ - '| cut -f3-
exit 1
fi
fi

使用 git diff --cached 查看将要提交的内容,并将它与初始空树进行比较。请注意,它将拒绝已经存在的 (但不变)二进制文件的提交;要使其仅拒绝新的或已更改的二进制文件,请在非ascii名称钩子中添加针对= 逻辑的。要仅仅拒绝 new 二进制文件,请同时添加 - diff-filter = A 参数。



<如果你喜欢,可以用更多的错误信息文字来表达它。你可以逆转测试(而不是必须断言停止二进制文件,使其默认停止,你必须设置allowbinaries来添加二进制文件)等。当然,你可以允许特定的目录充满二进制文件,或者其他方法,通过对diff-index输出进行额外的过滤。


Does anyone out there know of a good git hook that will check for binaries on commit and throw an error? I'm trying to prevent binaries from getting committed to my repository, but sometimes people make mistakes.

Thanks!

解决方案

I don't know of an existing hook, but git already comes with a hook that checks for adding "non-ascii names", as a sample pre-commit hook. This will likely already be in your existing git repositories as .git/hooks/pre-commit.sample.

Using that hook as a template and considering the answers to "How to determine if Git handles a file as binary or as text?", you could do something like this (see "git's semi-secret empty tree" for where EMPTY_TREE comes from):

#! /bin/sh

stop_binaries=$(git config --get hooks.stop_binaries)

exec 1>&2

if [ "$stop_binaries" = true ]; then
    EMPTY_TREE=$(git hash-object -t tree /dev/null)
    # or: EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
    if git diff --cached --numstat $EMPTY_TREE | grep -e '^-' >/dev/null; then
        echo Error: commit would add binary files:
        git diff --cached --numstat $EMPTY_TREE | grep -e '^-' | cut -f3-
        exit 1
    fi
fi

This uses git diff --cached to see what would be committed, comparing it all against an initial empty tree. Note that it will reject commits with already-existing (but unchanged) binaries; to make it only reject new or changed binaries, add the against= logic from the non-ascii-names hook. To reject only new binaries add the --diff-filter=A argument too.

Fancy this up with additional error-message text if you like. You can reverse the test (instead of having to assert "stop binaries", make it default to stop and you have to set "allowbinaries" to add binary files), etc. And of course you can allow specific directories full of binary files, or whatever, as well, by doing additional filtering on the diff-index output.

这篇关于有没有可以防止二进制检入的git钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆