Skip to content

Instantly share code, notes, and snippets.

@crazycode
Last active November 14, 2023 08:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crazycode/6ff6770ac502ab1843e8 to your computer and use it in GitHub Desktop.
Save crazycode/6ff6770ac502ab1843e8 to your computer and use it in GitHub Desktop.
安装Gerrit2以及使用说明

安装

参考: http://www.mediawiki.org/wiki/Gerrit/git-review

Ubuntu & Debian: sudo apt-get install git-review

Mac: sudo pip install git-review

使用

初始化

git review -s

建立项目分支

git checkout -b dev/feature1

.... modify code....

git ci

git review 流程推荐使用多行提交注释。

然后提交到gerrit:

git review

Vagrant配置

配置一下端口映射:

config.vm.network :forwarded_port, guest: 18080, host: 18080
config.vm.network :forwarded_port, guest: 29418, host: 29418

下载gerrit2和jdk安全文件

参考: https://gerrit-documentation.storage.googleapis.com/Documentation/2.9/install.html

MySQL操作

mysql> CREATE USER 'gerrit2'@'localhost' IDENTIFIED BY 'secret';
mysql> CREATE DATABASE gerrit2db default charset utf8 COLLATE utf8_general_ci;
mysql> GRANT ALL ON gerrit2db.* TO 'gerrit2'@'localhost';
mysql> FLUSH PRIVILEGES;

初始化Gerrit2

$ sudo adduser gerrit2
$ sudo su - gerrit2

初始化启动:

$ java -jar gerrit-2.9.war init -d /home/gerrit2/review_site

*** Gerrit Code Review 2.9
***

Create '/home/gerrit2/review_site' [Y/n]? Y

*** Git Repositories
***

Location of Git repositories   [git]:

*** SQL Database
***

Database server type           [h2]: mysql

Gerrit Code Review is not shipped with MySQL Connector/J 5.1.21
**  This library is required for your configuration. **
Download and install it now [Y/n]? Y
Downloading http://repo2.maven.org/maven2/mysql/mysql-connector-java/5.1.21/mysql-connector-java-5.1.21.jar ... OK
Checksum mysql-connector-java-5.1.21.jar OK
Server hostname                [localhost]:
Server port                    [(mysql default)]:
Database name                  [reviewdb]: gerrit2db
Database username              [gerrit2]:
gerrit2's password             :
              confirm password :

*** Index
***

Type                           [LUCENE/?]: ?
       Supported options are:
         lucene
         solr
Type                           [LUCENE/?]:

*** User Authentication
***

Authentication method          [OPENID/?]: ?
       Supported options are:
         openid
         openid_sso
         http
         http_ldap
         client_ssl_cert_ldap
         ldap
         ldap_bind
         custom_extension
         development_become_any_account
Authentication method          [OPENID/?]: development_become_any_account

*** Review Labels
***

Install Verified label         [y/N]?

*** Email Delivery
***

SMTP server hostname           [localhost]:
SMTP server port               [(default)]:
SMTP encryption                [NONE/?]:
SMTP username                  :

*** Container Process
***

Run as                         [gerrit2]:
Java runtime                   [/opt/jdk7/jre]:
Copy gerrit-2.9.war to /home/gerrit2/review_site/bin/gerrit.war [Y/n]?
Copying gerrit-2.9.war to /home/gerrit2/review_site/bin/gerrit.war

*** SSH Daemon
***

Listen on address              [*]:
Listen on port                 [29418]:

Gerrit Code Review is not shipped with Bouncy Castle Crypto SSL v149
  If available, Gerrit can take advantage of features
  in the library, but will also function without it.
Download and install it now [Y/n]? Y
Downloading http://www.bouncycastle.org/download/bcpkix-jdk15on-149.jar ... OK
Checksum bcpkix-jdk15on-149.jar OK

Gerrit Code Review is not shipped with Bouncy Castle Crypto Provider v149
** This library is required by Bouncy Castle Crypto SSL v149. **
Download and install it now [Y/n]? Y
Downloading http://www.bouncycastle.org/download/bcprov-jdk15on-149.jar ... OK
Checksum bcprov-jdk15on-149.jar OK
Generating SSH host key ... rsa... dsa... done

*** HTTP Daemon
***

Behind reverse proxy           [y/N]?
Use SSL (https://)             [y/N]?
Listen on address              [*]:
Listen on port                 [8080]: 18080
Canonical URL                  [http://review-ci:18080/]: http://gerrit.local.com:18080/

*** Plugins
***

Install plugin commit-message-length-validator version v2.9 [y/N]? y
Install plugin download-commands version v2.9 [y/N]? y
Install plugin replication version v2.9 [y/N]? y
Install plugin reviewnotes version v2.9 [y/N]? y
Install plugin singleusergroup version v2.9 [y/N]? y

Initialized /home/gerrit2/review_site
Executing /home/gerrit2/review_site/bin/gerrit.sh start
Starting Gerrit Code Review: OK
Waiting for server on gerrit.local.com:18080 ... OK
Opening http://gerrit.local.com:18080/#/admin/projects/ ...FAILED
Open Gerrit with a JavaScript capable browser:
  http://gerrit.local.com:18080/#/admin/projects/

启动方法

review_site/bin/gerrit.sh start
review_site/bin/gerrit.sh stop
review_site/bin/gerrit.sh restart

修改mysql编码

修改数据库连接参数,看gerrit的配置帮助吧,这里直接给结果吧:

[database]
    type = mysql
    url = jdbc:mysql://localhost:3306/reviewdb?user=user&password=pwd&useUnicode=true&characterEncoding=utf8

这样的话,中文便显示正常了。原来的配置是这样子的:

[database]
    type = mysql
    hostname = localhost
    database = reviewdb
    username = user

数据库要修改一下:

alter table account_diff_preferences engine=innodb;
alter table account_external_ids engine=innodb;
alter table account_group_id engine=innodb;
alter table account_group_includes_by_uuid engine=innodb;
alter table account_group_includes_by_uuid_audit engine=innodb;
alter table account_group_members engine=innodb;
alter table account_group_members_audit engine=innodb;
alter table account_group_names engine=innodb;
alter table account_groups engine=innodb;
alter table account_id engine=innodb;
alter table account_patch_reviews engine=innodb;
alter table account_project_watches engine=innodb;
alter table account_ssh_keys engine=innodb;
alter table accounts engine=innodb;
alter table change_id engine=innodb;
alter table change_message_id engine=innodb;
alter table change_messages engine=innodb;
alter table changes engine=innodb;
alter table patch_comments engine=innodb;
alter table patch_set_ancestors engine=innodb;
alter table patch_set_approvals engine=innodb;
alter table patch_sets engine=innodb;
alter table schema_version engine=innodb;
alter table starred_changes engine=innodb;
alter table submodule_subscriptions engine=innodb;
alter table system_config engine=innodb;
alter table tracking_ids engine=innodb;
ALTER TABLE account_diff_preferences CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_external_ids CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_group_id CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_group_includes_by_uuid CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_group_includes_by_uuid_audit CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_group_members CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_group_members_audit CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_group_names CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_groups CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_id CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_patch_reviews CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_project_watches CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE account_ssh_keys CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE accounts CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE change_id CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE change_message_id CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE change_messages CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE changes CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE patch_comments CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE patch_set_ancestors CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE patch_set_approvals CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE patch_sets CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE schema_version CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE starred_changes CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE submodule_subscriptions CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE system_config CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE tracking_ids CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

配置Apache2

网上的配置可能会出现不能打开Review界面,应该用下面这个配置, 注意AllowEncodedSlashes NoDecode和nocanon:

<VirtualHost *:10080>
    ServerAdmin webmaster@localhost
    ServerName gerrit.local.com
    ProxyRequests Off
    ProxyVia Off
    ProxyPreserveHost on
    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>
    AllowEncodedSlashes NoDecode
    ProxyPass / http://localhost:18080/ nocanon
</VirtualHost>

Nginx配置HttpAuth

安装及命令

git config --file $site_path/etc/gerrit.config auth.type HTTP

sudo apt-get install apache2-utils
sudo htpasswd -c /var/www/mywebsite.com/.htpasswd exampleuser

The tool will prompt you for a password.

New password:
Re-type new password:
Adding password for user exampleuser

Nginx配置文件

server {
  listen 80;
  server_name review.example.com;

  location / {
    proxy_pass        http://127.0.0.1:8081;
    proxy_set_header  X-Forwarded-For $remote_addr;
    proxy_set_header  Host $host;
    auth_basic "Restricted";                                #For Basic Auth
    auth_basic_user_file /var/www/mywebsite.com/.htpasswd;  #For Basic Auth
  }
}
供参考:
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/gerrit.log;
location / {
proxy_pass https://192.168.1.100:8080/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_read_timeout 300;
auth_basic "closed site";
auth_basic_user_file /etc/nginx/gerrit.htpasswd;
}
}

代码提交要求

  • 一个Commit对应一个修改点(除非是在项目初始阶段或是升级BSP时)。
  • 不要随便修改文件的属性。Windows 修改过的文件属性会变成755,上传代码时请注意修改成文件的原属性,可以用git whatchanged 来查看自己commit修改的文件。 缩进格式跟原文件保持一致。
  • 不要修改跟提交内容无关的代码。

Git commit 注释要求

  • 设置GIT使用vim来编辑commit,请不要使用-m参数 (git config –global core.editor vim)。
  • 注释内容应用英文写。
  • 如果代码是针对某个项目的改动,要以 项目名称+:+空格 开头,例如 D508: Add software navigator bar。
  • 首行粗略写下修改是什么内容,请注意不用太长。
  • 空一行之后详细写修改了什么,为什么要这么修改,如果是跟bug相关,请写上bug号。

例:

S100D: Cofigure GPIO31 and GPIO33 to INPUT PULL UP.

OEMSBL would check VOL+ and VOL- to enter download mode,
becase we only checked the low status, so we need set these two
GPIO to input pull up, otherwise, we can't get right status.

上传代码者

  • 修改代码,验证后提交到 Gerrit 上。
  • 代码提交后登陆 Gerrit,自己检查代码(重点看缩进格式跟原文件是否一致;去掉红色空格部分;修改内容是否正确;命名是否有意义;注释内容是否符合要求等)。 自己检查没问题后,点 “Review”按钮,在“Verified”中 +1,在“Code Review”中 +1,并点“Publish Comments”。
  • 在”Reviewer”栏中,加审核者。
  • 如果代码审核没有通过,请重复步骤1,2,3。

审核者

  • 收到邮件通知后登陆 Gerrit,审核代码。
  • 如果审核通过,点 “Review”按钮,在“Verified”中 +1,在“Code Review”中 +2,并点“Publish Comments”。
  • 如果代码审核没有通过,点 “Review”按钮,在“Code Review”中 -2,写好评论后,点“Publish Comments”。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment