谷歌的 Android 开源项目在 Git 的使用上有两个重要的创新,一个是为多版本库协同而引入的 repo,另一个重要的创新就是 Gerrit —— 代码审核服务器。Gerrit 为 git 引入的代码审核是强制性的,就是说除非特别的受权设置,向 Git 版本库的推送(Push)必需要通过 Gerrit 服务器,修订必须通过代码审核的一套工做流以后,才可能经批准并归入正式代码库中。java
首先贡献者的代码经过 git 命令(或git review封装)推送到 Gerrit 管理下的 Git 版本库,推送的提交转化为一个一个的代码审核任务,审核任务能够经过 refs/changes/下的引用访问到。代码审核者能够经过 Web 界面查看审核任务、代码变动,经过 Web 界面作出经过代码审核或者打回等决定。测试者也能够经过 refs/changes/引用获取(fetch)修订对其进行测试,若是测试经过就能够将该评审任务设置为校验经过(verified)。最后通过了审核和校验的修订能够经过 Gerrit 界面中提交动做合并到版本库对应的分支中。更详细的流程描述见下图所示: mysql
sudo adduser gerrit #给用户添加sudo权限 chmod u+w /etc/sudoers sudo vi /etc/sudoers #在root ALL=(ALL) ALL添加下面一行 gerrit ALL=(ALL) ALL su gerrit
安装Gerrit须要装有最低1.6版本的JDK:android
sudo apt-get install default-jre sudo apt-get install git
https://code.google.com/p/gerrit/
https://gerrit-releases.storage.googleapis.com/gerrit-2.12.warnginx
java -jar gerrit-2.11.war init -d review_site
Gerrit支持H2(内置) / MySQL / PostgreSQL数据库,简单使用默认数据库H2,mysql和postgreSQL数据库在认证人数比较多时选用.
Gerrit支持OpenID / HTTP / LDAP, 认证方式没有选择OpenId, 而是http, 由于这样会使得gerrit对外部系统有依赖, 目前gerrit支持google和yahoo提供的openid.
选择http须要反向代理支持, 这和http认证有关.
LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,通常都简称为LDAP
配置文件review_site/etc/gerrit.config
,邮箱密码存在review_site/etc/secure.config
文件中.git
vi ./review_site/etc/gerrit.config
#将canonicalWebUrl修改成代理服务器地址
[gerrit]
basePath = /home/gerrit/repositories
canonicalWebUrl = http://localhost:8090/ [database] type = postgresql hostname = localhost database = reviewdb username = gerrit [index] type = LUCENE [auth] type = HTTP [sendemail] enable = true smtpServer = smtp.163.com smtpServerPort = 25 smtpUser = your_name@163.com from = gerrit<your_name@163.com> [sshd] listenAddress = *:29418 [httpd] listenUrl = proxy-http://*:8081/ [cache] directory = cache
vi etc/secure.config [database] password = your_password [auth] registerEmailPrivateKey = your_password restTokenPrivateKey = your_password [sendemail] smtpPass = your_password
nginx做为代理服务器更加方便,在/etc/nginx/sites-enabled
添加一个server模块web
server {
listen *:8090; server_name localhost; location / { auth_basic "Welcomme to Gerrit Code Review Site"; #确保passwd路径正确 auth_basic_user_file /home/gerrit/review_site/etc/passwd; proxy_pass http://localhost:8081; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; } location /login/ { proxy_pass http://localhost:8081; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; } }
touch ./review_site/etc/passwd
#添加gerrit帐号 htpasswd -b ./review_site/etc/passwd yourname yourpassword #重启gerrit,帐号才会生效 ./review_site/bin/gerrit.sh restart
第一次成功登陆的用户会被gerrit做为管理员用户。登陆后点击右上角的”匿名懦夫”Anonymous Coward -> Settings来配置帐户。
添加SSH公钥
要使用gerrit必需要提供用户的公钥。选择页面左侧的SSH Public Keys为当前用户添加公钥。直接将公钥粘贴到Add SSH Public Key框内,而后点击add便可。sql
若是采用http认证,那么添加其余帐户时,须要现添加http认证帐户。用htpasswd建立的用户时,并无往gerrit中添加帐号,只有当该用户经过web登录gerrit服务器时,该帐号才会被添加进gerrit数据库中。shell
也行你会发现用gerrit+HTTP认证,经过web登录后,点击右上角的Sign Out没法登出。要么是依然保持登录的状态,要么就是直接出错。
不要觉得怎么了,其实这是正常现象,如下这段话是从网上看到的:You are using HTTP Basic authentication. There is no way to tell abrowser to quit sending basic authentication credentials, to logout with basicauthentication is to close the Webbrowser.数据库
#默认使用.ssh/id_rsa.pub公钥
ssh -p 29418 -i admin@localhost **** Welcome to Gerrit Code Review **** Hi admin, you have successfully connected over SSH. Unfortunately, interactive shells are disabled. To clone a hosted Git repository, use: git clone ssh://admin@learnLinux:29418/REPOSITORY_NAME.git Connection to localhost closed.
新建一个gerritRepo仓库,git clone http://127.0.0.1:8080/gerritRepo
在推送时apache
remote: Unauthorized fatal: Authentication failed for 'http://admin@127.0.0.1:8080/gerritRepo/'
改用ssh方式push
git remote remove origin
git remote add origin ssh://admin@127.0.0.1:29418/gerritRepo git push origin master
remote: Branch refs/heads/master:
remote: You are not allowed to perform this operation. remote: To push into this reference you need 'Push' rights. remote: User: member remote: Please read the documentation and contact an administrator remote: if you feel the configuration is incorrect remote: Processing changes: refs: 1, done To ssh://member@127.0.0.1:29418/hello1 ! [remote rejected] master -> master (prohibited by Gerrit) error: 没法推送一些引用到 'ssh://member@127.0.0.1:29418/hello1'
这就是gerrit的精髓所在了。缘由是gerrit不容许直接将本地修改同步到远程仓库。客户机必须先push到远程仓库的refs/for/*分支上,等待审核。这也是为何咱们须要使用gerrit的缘由。gerrit自己就是个代码审核工具。
#提交master分支 git push origin HEAD:refs/for/master #提交全部分支 git push origin refs/heads/*:refs/for/* #修改.git/config文件,添加push时的引用 [remote "origin"] url = ssh://chenjianhua@127.0.0.1:29418/hello1 fetch = +refs/heads/*:refs/remotes/origin/* push = HEAD:refs/for/*
再次推送到服务器
remote: Processing changes: refs: 1, done remote: ERROR: missing Change-Id in commit message footer remote: remote: Hint: To automatically insert Change-Id, install the hook: remote: gitdir=$(git rev-parse --git-dir); scp -p -P 29418 root@ubuntu:hooks/commit-msg ${gitdir}/hooks/ remote: And then amend the commit: remote: git commit --amend remote: To ssh://member@127.0.0.1:29418/hello1 ! [remote rejected] master -> refs/for/master (missing Change-Id in commit message footer) error: 没法推送一些引用到 'ssh://member@127.0.0.1:29418/gerritRepo'
push时提示须要Change-Id
在提交信息中, 须要从gerrit server上下载一个脚本
钩子的目的是在提交信息中自动建立 ‘Change-Id:’ 标签
scp -p -P 29418 admin@127.0.0.1:hooks/commit-msg gerritRepo/.git/hooks/ #修改上次提交记录,或者再次提交修改 git commit --amend remote: Processing changes: new: 1, refs: 1, done remote: remote: New Changes: remote: http://localhost:8081/2 vi README remote: To ssh://member@127.0.0.1:29418/gerritRepo * [new branch] master -> refs/for/master
给refs/head/*分支Label Verified权限添加用户分组,这里分配Administrators组.
项目评审过程当中,须要几个条件,代码才能最终提交到分支
评审过程一般有三我的参与,代码提交,代码验证(Verify),代码审查(Review). 一般由自动测试工具jenkins完成代码验证(Verify).
开发者的代码须要先提交到refs/for/master分支上,变更的代码称做补丁集,保存在 refs/changes/*
命名空间下.
git ls-remote
From ssh://admin@localhost:29418/gerrit_ci 5f8ed98b0f88787c22e705595e2818db62874f56 HEAD eeaef9da4ea27d7c23bfb5f9a2ed1b5357ebbba8 refs/changes/01/1/1 5f8ed98b0f88787c22e705595e2818db62874f56 refs/changes/02/2/1 bfdb700f4aab3afc32ec79a29b0e25f8be758f8f refs/changes/03/3/1 5f8ed98b0f88787c22e705595e2818db62874f56 refs/heads/master 887107fcb25c48d1a1eb116ec466fc4f9b298a5c refs/meta/config 21be8fce8a38d9437363128d214739c64bdd5710 refs/notes/review #下载补丁 git fetch ssh://admin@localhost:29418/gerrit_ci refs/changes/03/3/1
sudo apt-get install postgresql #次安装后,会默认生成名为postgres的Linux系统用户、数据库和数据库用户(做为数据库管理员),首先修改postgres数据库用户的密码,而后增长Gerrit须要的数据库 #切换到postgres用户 sudo su postgres #登陆postgres数据库 psql postgres #修改postgres用户登陆密码 ALTER USER postgres with PASSWORD 'password' #输入密码 postgres=# #输入第二遍密码 postgres=# \q #建立gerrit用户 CREATE USER gerrit WITH PASSWORD 'password'; #建立数据库 CREATE DATABASE reviewdb OWNER gerrit; #将reviewdb全部权限赋予gerrit GRANT ALL PRIVILEGES ON DATABASE reviewdb to gerrit;
#vi etc/gerrit.config [database] type = postgresql hostname = localhost database = reviewdb username = gerrit #vi etc/secure.config [database] password = password
#链接数据库
mysql -u root -p
#查看帮助
help contents;
help Administration;
#建立gerrit用户和reviewdb数据库
CREATE USER 'git'@'localhost' IDENTIFIED BY 'git'; CREATE DATABASE reviewdb; ALTER DATABASE reviewdb charset=latin1; GRANT ALL ON reviewdb.* TO 'git'@'localhost'; FLUSH PRIVILEGES; #查看全部数据库 SHOW DATABASES; #查看全部用户 SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
*** 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)]: 3306 Database name [reviewdb]: reviewdb Database username [gerrit]: gerrit gerrit's password : confirm password :
也能够将mysql-connector-Java-5.1.21.jar
放入lib目录下