Friday, August 23, 2013

Gerrit

To those of you who are new to Gerrit, Gerrit[1] is a free, web-based team code review tool which can be used by members of a team to review each other's modifications in their source code using a web browser and approve or reject those changes. It integrates closely with Git.

Prerequisite
  • Java is needed to install and run Gerrit. If you don't have Java installed, then download and install the latest JDK on your machine.
  • Create a user named gerrit2 to host gerrit services.
    $ sudo adduser gerrit2
Installation
  • To install gerrit, first change to gerrit user
    $ sudo su - gerrit
  • Iinitialize with the batch switch enabled, so that we don't have to answer any questions.
    $ java -jar gerrit.war init --batch -d <site-path>
    site-path: lets assume that it is ~/gerrit_testsite
         This is automatically start gerrit on localhost running on port 8080.


Configuration
  • You may wish to change the port on which gerrit runs on. To do this, edit the gerrit_testsite/etc/gerrit.config and replace the port number 8080 with the port of your choice. In this document we will stick with the port number 8080.
  • Now restart the gerrit process.
    $ ~/gerrit_testsite/bin/gerrit.sh restart

Registering User
  • Open the browser and enter the url www.localhost:8080 and it will prompt you to register/sign in. You can use open-id to sign in. In Gerrit, the first user to register becomes the default administrator.

  • On the registering site, add the public key of the machine from which you 'll send patches to.
  • Now you are all set. Try loging in using ssh, gerrit uses port 29418 for ssh access.
    $ ssh -p 29418 john@localhost
The authenticity of host '[localhost]:29418 ([127.0.0.1]:29418)' can't be established.
RSA key fingerprint is 35:72:c5:2d:0c:16:8a:68:04:ab:2f:51:f1:d0:0c:6b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:29418' (RSA) to the list of known hosts.
***    Welcome to Gerrit Code Review    ****
Hi john, you have successfully connected over SSH.
Unfortunately, interactive shells are disabled.
To clone a hosted Git repository, use:
git clone ssh://john@localhost:29418/REPOSITORY_NAME.git
Connection to localhost closed.

Creating a Project
Gerrit gives the flexibility to either create a new project from scratch or upload an existing project to gerrit.
  • To create a new project from scratch, create a new repository on the gerrit machine by passing the option empty-project.
    $ ssh -p 29418 john@localhost gerrit create-project --empty-project -name test-proj
    In our case, we created an empty project named test-proj.
  • Alternatively, you can tell gerrit that you will be uploading an existing project. To do this, first create a project on the gerrit machine.
    $ ssh -p 29418 john@localhost gerrit create-project name test-proj
    This command will create a git repository on the gerrit machine. Behind the scenes, the above command runs 
    $ mkdir test-proj.git
    $ cd test-proj.git
    $ git init --bare --shared
    Once the project is created, add a remote repository reference to your local repository.
    $ git remote add origin ssh://john@localhost:29418/test-proj.git
    Now push your changes to the upstream repository
    $ git push origin
    Counting objects: 5, done.
    Writing objects: 100% (3/3), 248 bytes, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To ssh://john@localhost:29418/test-proj.git
    ! [remote rejected] master -> master (prohibited by Gerrit)
    error: failed to push some refs to 'ssh://john@localhost:29418/test-proj.git'
The above error is because the user is trying to push the changes to the upstream repository directly, bypassing the gerrit's review process.
  • Gerrit maintains a seperate change branch to which the new changes would be committed and made available for review to other users.
    $ git push origin HEAD:refs/for/master
Hear master is the branch name to which the changes have to be applied to. Replace it with the branch name to which you want to apply your changes to.

Clone Project
  • Users who wish to clone the project must first register to the project and then clone using ssh.
    $ git clone ssh://smith@localhost:29418/test-proj.git
Permissions
    • Owner
    • Push Merge Commit
    • Label Code-Review
    • Label Verified
    • Submit
There are various types of groups like Administrator, Registered users, Anonymous, etc. Apart from this, you can explicitly a new group and add members to this newly created group and grant permissions to them.
  • Now that the permssions are all set, users can clone the repo, make changes locally and push the changes to review.
    $ git clone ssh://steve@localhost:29418/test-proj.git
    $ cd test-proj
      ---- make changes and add the changed files to git ----
    $ git commit
    $ git push origin HEAD:refs/for/<branch-name>

No comments:

Post a Comment