Back to Code Review

See Also Source Control ManagementGitGitlabOpenLDAP

Gerrit

Gerrit provides web based code review and repository management for the Git version control system.

1. Tutorial

1.1. A Quick Introduction

https://gerrit-documentation.storage.googleapis.com/Documentation/2.13.3/intro-quick.html

intro-quick-central-gerrit.png

1.2. Setup the Server

https://gerrit-documentation.storage.googleapis.com/Documentation/2.13.3/install.html

1.2.1. Download the war

https://gerrit-releases.storage.googleapis.com/index.html

wget https://www.gerritcodereview.com/download/gerrit-2.13.3.war

1.2.2. Database Setup

mysql> CREATE USER 'gerrit2'@'127.0.0.1' IDENTIFIED BY 'secret';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE DATABASE reviewdb
       DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL ON reviewdb.* TO 'gerrit2'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

1.2.3. Initialize the Site

# adduser gerrit2
# mkdir -p /var/lib/gerrit2
# chown gerrit2:gerrit2 /var/lib/gerrit2
# su - gerrit2
$ java -jar gerrit-2.13.3.war init -d /var/lib/gerrit2
...
Location of Git repositories   [git]: /var/lib/git_repo
...

Initialized /var/lib/gerrit2

1.2.4. Start/Stop Daemon

[gerrit2@localhost gerrit2]$ bin/gerrit.sh status
Checking arguments to Gerrit Code Review:
  GERRIT_SITE     =  /var/lib/gerrit2
  GERRIT_CONFIG   =  /var/lib/gerrit2/etc/gerrit.config
  GERRIT_PID      =  /var/lib/gerrit2/logs/gerrit.pid
  GERRIT_TMP      =  /var/lib/gerrit2/tmp
  GERRIT_WAR      =  /var/lib/gerrit2/bin/gerrit.war
  GERRIT_FDS      =  1024
  GERRIT_USER     =  gerrit2
  JAVA            =  /usr/local/jdk1.7/jre/bin/java
  JAVA_OPTIONS    =  
  RUN_EXEC        =  /usr/bin/perl -e '$x=$ENV{JAVA};exec $x @ARGV;die $!' -- GerritCodeReview
  RUN_ARGS        =  -jar /var/lib/gerrit2/bin/gerrit.war daemon -d /var/lib/gerrit2

1.2.5. Reverse Proxy

https://gerrit-documentation.storage.googleapis.com/Documentation/2.13.3/config-reverseproxy.html

1.2.6. Authenticate with LDAP

https://gerrit-review.googlesource.com/Documentation/config-gerrit.html#ldap

http://mageconfig.blogspot.sg/2014/06/configure-gitgerrit-with-open-ldap-for.html

Crowd LDAP Server integrate with atlassian crowd server

[auth]
        type = LDAP
[ldap]
        server = ldap://localhost:10389
        accountBase = ou=users,dc=crowd
        groupBase = ou=groups,dc=crowd
        username = dn=jira,ou=users,dc=crowd
        referral = follow
        accountPattern = (uid=${username})
        groupPattern = (cn=${groupname})
        accountFullName = cn
        accountEmailAddress = mail

1.2.7. 权限管理

http://m.blog.csdn.net/article/details?id=50571190

1.3. Talk to Gerrit via SSH/HTTP

Add public key in http://<host>/gerrit/#/settings/ssh-keys

➜  pages cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCgKo/hnehM7bw4M6uawnWhhsvGlpnmM+PhKg8TqBoYrg5xochWEHFhQobibcQfNKvyxfoviGtLFp2LT1wrCombyF2djBX/4BsP4I6GLC36ynRREpXzZdv4Pfsg590wTsj1pES8OfWxwQ1hQS6sLr+O+3iwrOhGSO1F/XCLyqIg7hdAs87ILpsWS0eIgQLuHZ1QGEjWZLF5iW8UTKP3Z9Az6rVtIOsFU8yCftOFO+UR2FRcvwHlU6fp/g4+a7PCTUxD7PytM84mRDO4Cr9/yN2JBkejYZfuA5oXcI+n7iPimegL1w1H+fB4vb3AokE5kWMGIQlHcpXN20NGonH0HFfT liyan@facebook.com
➜  pages ssh -p29418 172.16.100.90 #jenkins

  ****    Welcome to Gerrit Code Review    ****

  Hi 三火, you have successfully connected over SSH.

  Unfortunately, interactive shells are disabled.
  To clone a hosted Git repository, use:

  git clone ssh://liyan@172.16.100.90:29418/REPOSITORY_NAME.git

Connection to 172.16.100.90 closed.

Set HTTP Password:

1.4. Create a Gerrit Project

https://gerrit-documentation.storage.googleapis.com/Documentation/2.13.3/cmd-create-project.html

➜  tmp ssh -p29418 172.16.100.90 gerrit
➜  tmp ssh -p29418 172.16.100.90 gerrit create-project sandbox/hello_world
➜  /tmp git clone ssh://172.16.100.90:29418/sandbox/hello_world
Cloning into 'hello_world'...
Checking connectivity... done.
➜  /tmp cd hello_world                                         
➜  hello_world git:(master) git remote -v
origin  ssh://172.16.100.90:29418/sandbox/hello_world (fetch)
origin  ssh://172.16.100.90:29418/sandbox/hello_world (push)
➜  hello_world git:(master) git remote add gerrit ssh://172.16.100.90:29418/sandbox/hello_world
➜  hello_world git:(master) git remote -v
gerrit  ssh://172.16.100.90:29418/sandbox/hello_world (fetch)
gerrit  ssh://172.16.100.90:29418/sandbox/hello_world (push)
origin  ssh://172.16.100.90:29418/sandbox/hello_world (fetch)
origin  ssh://172.16.100.90:29418/sandbox/hello_world (push)

1.5. Gerrit Change-Id

https://gerrit-documentation.storage.googleapis.com/Documentation/2.13.3/user-changeid.html#create

➜  hello_world git:(master) curl -Lo .git/hooks/commit-msg http://172.16.100.90/gerrit/tools/hooks/commit-msg
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4682  100  4682    0     0  28603      0 --:--:-- --:--:-- --:--:-- 28723
➜  hello_world git:(master) chmod u+x .git/hooks/commit-msg
➜  hello_world git:(master) git log -1
commit 255317a3d12d0c50c25a5a21c2d5caf455a24804
Author: twotwo <twotwo.li@gmail.com>
Date:   Tue Dec 13 18:24:03 2016 +0800

    update README.md
    
    Change-Id: Ic39f7cadac6d2faa2306faa7f261404bfc00b286

1.6. Submit changes for review

https://gerrit-documentation.storage.googleapis.com/Documentation/2.13.3/user-upload.html

➜  hello_world git:(master) git push origin HEAD:refs/for/master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 380 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Processing changes: new: 1, refs: 1, done    
remote: 
remote: New Changes:
remote:   http://172.16.100.90/gerrit/1 add README.md
remote: 
To ssh://172.16.100.90:29418/sandbox/hello_world
 * [new branch]      HEAD -> refs/for/master

1.7. Verify Changes

verify_changes.png

1.8. Submit the Change

2. Plugins

2.1. Building with Buck

https://review.gerrithub.io/Documentation/dev-buck.html

➜  java git clone https://github.com/facebook/buck
➜  java cd buck
➜  buck git checkout $(cat ../gerrit/.buckversion)
➜  buck ant
➜  buck ln -s `pwd`/bin/buck ~/app/bin/
➜  buck ln -s `pwd`/bin/buckd ~/app/bin/

2.2. Project plugins/delete-project

https://gerrit-review.googlesource.com/#/admin/projects/plugins/delete-project

http://sychen.logdown.com/posts/2014/12/28/delete-project-in-gerrit

➜  /tmp git clone https://gerrit.googlesource.com/plugins/delete-project
➜  /tmp cd delete-project
➜  delete-project buck build plugins/delete-project:delete-project

3. Reference


CategoryTool

MainWiki: Gerrit (last edited 2016-12-06 23:44:21 by twotwo)