如何 install/config Git::Hooks 与 Gerrit 一起使用

How to install/config Git::Hooks to use with Gerrit

Git::Hooks 是一个用于实现 Git/Gerrit 挂钩的 Perl 框架。安装和配置它以与 Gerrit 一起使用的过程是什么?

安装

1) 安装依赖:

Ubuntu:

sudo apt-get install gcc make libcrypt-ssleay-perl

红帽:

sudo yum install gcc make libcrypt-ssleay-perl

2) 安装 Git::Hooks:

sudo cpanm Git::Hooks
sudo cpanm Gerrit::REST
sudo cpanm JIRA::REST

配置

0) 确保在 Gerrit 中安装了 hooks plugin

1)在GERRIT-SITE/hooks目录下创建Gerrit hooks patchset-created and draft-published(有执行权限),内容如下:

#!/usr/bin/env perl
use Git::Hooks;
$ENV{HOME}="/home/GERRIT-USER";
run_hook([=13=], @ARGV);

2) 配置 Gerrit 存储库:

任何存储库都可以单独设置,但在 GERRIT-SITE/git/all-projects.git/config 文件中配置所有项目更容易,如下例所示(一些配置项目可以是 added/removed 作为需要):

...
[githooks]
    plugin = CheckJira
    plugin = CheckLog
    plugin = CheckFile
    nocarp = 1
[githooks "gerrit"]
    url = https://GERRIT-SERVER
    username = GERRIT-USER
    password = GERRIT-PASS
    votes-to-approve = Verification+1
    votes-to-reject = Verification-1
    comment-ok = OK
    notify = OWNER
[githooks "checkjira"]
    jiraurl = https://JIRA-SERVER
    jirauser = JIRA-USER
    jirapass = JIRA-PASS
    matchlog = (?s)^(?:Revert\s\")?\s*\[([^]]+)\]
    status = In Progress
    status = Analysing
    status = Treating
    status = Evaluating
    status = Waiting Information
[githooks "checklog"]
    title-required = 1
    title-max-width = 60
    body-max-width = 80
[githooks "checkfile"]
    sizelimit = 5242880
    name = *.p[lm] /usr/local/bin/perlcritic --stern --verbose 5
    name = *.pp /usr/local/bin/puppet-lint --error-level error --with-context --with-filename
    name = *.sh LANG=en_US.UTF-8 /usr/bin/shellcheck --exclude=SC2046,SC2053,SC2086
    name = *.erb /usr/bin/erb -P -x -T - {} | ruby -c
...

然后通过在 GERRIT-SITE/git/REPO.git/config

添加以下内容,将所有项目配置包含在所有存储库中
[include]
    path = GERRIT-SITE/git/all-projects.git/config

如果需要,任何配置都可以 changed/added 在某些特定的存储库中更改 GERRIT-SITE/git/repo。git/config 如下例所示(某些配置可以 added/removed 作为需要):

...
[githooks]
    plugin = CheckWhitespace
[githooks "checkjira"]
    require = 0
    unresolved = 0
...

要了解有关 Git::Hooks 配置选项的更多信息,请参阅 here

3) 重启 Gerrit 服务器

GERRIT-SITE/bin/gerrit.sh restart