2

我已经开始使用 Gerrit 2.16 作为代码审查工具,并且想要配置服务器端挂钩以在将更改提交/推送到 gerrit 时验证 git 提交消息。

尝试通过将脚本复制到 $GIT_DIR/hooks(ref-update、patchset-created、change-merged 等脚本)来使用钩子,在 gerrit 服务器上授予权限,但没有任何效果。

可以通过在 gerrit UI 中使用命令在本地存储库上启用 commit-msg 钩子

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

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

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

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

4

3 回答 3

4

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

Gerrit 不会在它使用的存储库中运行任何标准的 git 钩子,但它确实具有通过Hooks 插件包含的自己的钩子机制。有关支持的 Gerrit 钩子的更多信息,请参见此处。Hooks 插件是一个核心插件(它被打包在 Gerrit war 文件中,可以在 Gerrit 初始化期间轻松安装)。

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

于 2018-01-20T17:26:17.117 回答
1
#!/bin/bash
echo "Executing hook from Gerrit_DIR "
bold=$(tput bold)
normal=$(tput sgr0)
RED='\033[0;31m'
NC='\033[0m' # No Color
GIT_DIR="/opt/gerrit/site/git"
PROJECT=$2
REFNAME=$4
UPLOADER=$6
OLDREV=$8
NEWREV="${10}"
BASE="<baseDir>"
RepoDir="$GIT_DIR/$PROJECT.git"
echo $RepoDir
echo $PROJECT
echo $8
echo ${10}


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

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
于 2018-01-24T16:41:03.433 回答