如何在 Gerrit 2.16 中配置服务器端挂钩

How to configure server side hooks in Gerrit 2.16

我已经开始使用 Gerrit 2.16 作为代码审查工具,并希望配置服务器端挂钩以在 committed/pushed 对 gerrit 进行更改时验证 git 提交消息。

尝试通过将脚本复制到 $GIT_DIR/hooks(参考更新、补丁集创建、更改合并等脚本)来使用挂钩,在 gerrit 服务器上授予权限,但没有任何效果。

commit-msg 挂钩可以在本地存储库上使用命令 give in gerrit UI

示例: git clone ssh://@:29418/Project1 && scp -p -P 29418 @:hooks/commit-msg /.git/hooks/

change_ID 如果启用此挂钩,将自动生成。

执行上述命令时,此脚本 commit-msg 会下载到本地存储库。
我的问题;我们可以在 gerrit 服务器上找到这个脚本的路径,以便我可以修改和强制执行 git 提交消息验证吗?

或者有没有其他方法可以启用 gerrit 服务器端挂钩?

不,您不会在 Gerrit 服务器上找到 commit-msg 路径,并且 Git/Gerrit 不会自动使用您放置在 $GIT_DIR/hooks 上的任何挂钩。如果你想要本地挂钩,你需要在 REPOSITORY/.git/hooks 本地目录中手动安装它们。

Gerrit 没有 运行 它使用的存储库中的任何标准 git 挂钩,但它确实有自己的挂钩机制,通过 Hooks plugin. See here more info about the supported Gerrit hooks. The Hooks plugin is a core plugin 包含(它打包在Gerrit war 文件,可以在 Gerrit 初始化期间轻松安装)。

我建议您看一下 (一个用于实现 Git/Gerrit 挂钩的 Perl 框架)。我们在我们公司使用,它真的很棒。您可以使用它来实现您想要的以及更多...

#!/bin/bash
echo "Executing hook from Gerrit_DIR "
bold=$(tput bold)
normal=$(tput sgr0)
RED='3[0;31m'
NC='3[0m' # No Color
GIT_DIR="/opt/gerrit/site/git"
PROJECT=
REFNAME=
UPLOADER=
OLDREV=
NEWREV=""
BASE="<baseDir>"
RepoDir="$GIT_DIR/$PROJECT.git"
echo $RepoDir
echo $PROJECT
echo 
echo 


# execute the project specific hook
if [ -f "$RepoDir/hooks/commit-received" ]
    then
    echo "Executing the project specific hook"
    $RepoDir/hooks/commit-received $RepoDir 
else
    echo "There is no project specific hook"
fi
#!/bin/bash
echo "Executing hook for patchset"
bold=$(tput bold)
normal=$(tput sgr0)
RED='3[0;31m'
NC='3[0m' # No Color
RepoDir=
NEWREV=
MSG=$(git --git-dir=$RepoDir log --format=%B -n 1 $NEWREV | awk -F"Change-Id:" '{print }')

Val=`echo $MSG | cut -c1-3`
if  [ $Val == "TR_" ] || [ $Val == "CR_" ] || [ $Val == "PU_" ]  || [ $Val == "FR_" ] || [ $Val == "CSR" ]
    then
    echo "The commit message is valid"
    exit 0

else    
    echo -e "The commit message ${RED}\"$MSG\"${NC} is not valid, please enter a valid commit message"
    exit 1
fi