有没有什么好方法可以限制git/gerrit上一次代码更改的大小

有没有什么好方法可以限制git/gerrit上一次代码更改的大小,git,continuous-integration,gerrit,Git,Continuous Integration,Gerrit,我们有一个过程来限制一个代码检查/提交大小,并希望内置工具来检查和阻止所有超出限制的提交。 基于git/gerrit插件和谷歌搜索,没有看到任何现有的解决方案。是否有任何做法或建议在git/gerrit上增加此类限制 非常感谢 Gerrit有这样的想法 它可以通过全局配置 对于特定项目,您可以在项目的常规页面上找到最大Git对象大小限制: 更新: 如果您想检查Gerrit中更改的行数,我建议您使用Gerrit hookref update。它的工作原理有点像Git hookupdate。钩子通过

我们有一个过程来限制一个代码检查/提交大小,并希望内置工具来检查和阻止所有超出限制的提交。 基于git/gerrit插件和谷歌搜索,没有看到任何现有的解决方案。是否有任何做法或建议在git/gerrit上增加此类限制

非常感谢

Gerrit有这样的想法

它可以通过全局配置

对于特定项目,您可以在项目的
常规
页面上找到
最大Git对象大小限制:

更新:

如果您想检查Gerrit中更改的行数,我建议您使用Gerrit hook
ref update
。它的工作原理有点像Git hook
update
。钩子通过
--oldrev--newrev
接收旧提交和新提交。参数列表在Gerrit的不同版本中可能有所不同。有关详细信息,请参阅您的Gerrit文件。如果挂钩以非零值退出,则推送将被拒绝

示例代码:

#!/bin/bash

z40=0000000000000000000000000000000000000000

while test $# != 0
do
  case $1 in
  --oldrev)
      shift
      oldrev=$1
  ;;
  --newrev)
      shift
      newrev=$1
  ;;
  esac
  shift
done

if [[ "${newrev}" = ${z40} ]];then
    # Handle delete
    # Do something here

else
    if [[ "${oldrev}" = ${z40} ]];then
        # New branch
        # Do something here

    else
        # Update existing branch, check new commits
        newcommits=$(git log --pretty=%H ${oldrev}..${newrev})
        for commit in ${newcommits};do
            shortstat=$(git show ${commit} --pretty="" --shortstat)
            # Parse shortstat to get the numbers of deletions and insertions
            # if the number is big enough, print error message and exit with a non-zero value, 
            # so that git-push will be rejected

        done
    fi
fi
exit 0
#!/bin/bash

shortstat=$(git diff --cached --shortstat)
# parse shortstat to get the numbers of deletions and insertions
# if the number is big enough, print error message and exit with a non-zero value, 
# so that git-commit will fail
如果您想在本地存储库中检查它,我建议使用Git hook
pre-commit

示例代码:

#!/bin/bash

z40=0000000000000000000000000000000000000000

while test $# != 0
do
  case $1 in
  --oldrev)
      shift
      oldrev=$1
  ;;
  --newrev)
      shift
      newrev=$1
  ;;
  esac
  shift
done

if [[ "${newrev}" = ${z40} ]];then
    # Handle delete
    # Do something here

else
    if [[ "${oldrev}" = ${z40} ]];then
        # New branch
        # Do something here

    else
        # Update existing branch, check new commits
        newcommits=$(git log --pretty=%H ${oldrev}..${newrev})
        for commit in ${newcommits};do
            shortstat=$(git show ${commit} --pretty="" --shortstat)
            # Parse shortstat to get the numbers of deletions and insertions
            # if the number is big enough, print error message and exit with a non-zero value, 
            # so that git-push will be rejected

        done
    fi
fi
exit 0
#!/bin/bash

shortstat=$(git diff --cached --shortstat)
# parse shortstat to get the numbers of deletions and insertions
# if the number is big enough, print error message and exit with a non-zero value, 
# so that git-commit will fail

Gerrit钩子更可靠,因为每次要检查的新提交都会调用它,但它可能会影响Gerrit服务的性能。Git钩子可能被有意或意外地绕过,但它在本地运行。此外,我不考虑样本中的二进制文件。它们的删除和插入不能反映实际的增量大小。

ElpieKay,谢谢你的建议。但我们想检查行代码的数量变化,而不是对象的大小。maxObjectSizeLimit用于指定对象文件大小,而不是代码更改增量大小。对不起,我没有说清楚。@arden我更新了答案,提供了两种可能的解决方案。