Dcycle Blog

Setting up Gerrit with a git repo

December 04, 2014

Gerrit is a free open-source code review platform created by Google. In this post we will set up Gerrit with a Git repo.

Step 1: install Gerrit

See this post for instructions on how to install Gerrit on CentOS. We will assume that your Gerrit instance is available at http://gerrit.example.com:8080.

Step 2: Set the canonical URL

Make sure the canonical web url is correctly set to http://gerrit.example.com:8080. This is required to log into the web interface.

vi /srv/gerrit/etc/gerrit.config

Once that is done, restart gerrit

/srv/gerrit/bin/gerrit.sh restart

Step 3: log into Gerrit

Gerrit relies on OpenID, and because of this bug Google does not qualify as of this writing. You have to use an alternate “supported” OpenID provider such as Yahoo, which we’ll use here: open a Yahoo account which you will use to log into Gerrit. To create the first account, visit your Gerrit front page and create your account by clicking “Register”.

Add your name and your public ssh key.

It is important to set your username as well in order to be able to clone and push git repos.

Important note: I could not figure out how to disable registration of new users to Gerrit, so it is important to set access rules at the project level.

Step 4: Understanding how Gerrit works.

Gerrit seems to be designed to manage git repos, rather than tack onto an existing workflow (like Phabricator). I looked this up and it seems that Gerrit requires you to host your Git repo on the Gerrit server itself.

If you are already hosting your git repos on Stash, or Gitolite, or GitHub, you might want to experiment using mirroring or some other technique, but for this demo I will make Gerrit the canonical git provider for my repo.

According to Using Gerrit and Git (video), reviews are at the commit level (5 commits = 5 reviews), which may or may not be in line with your shop’s practices. The idea is that we want to force developers to squash commits into valuable units for better commit log. We’ll see how later on.

Step 5: Move your canonical git repo to Gerrit

Wherever your current git repo is now, the only way I could get Gerrit to work was to move the Git repo to Gerrit, although there is a discussion about this (and maybe some workaround).

So: first thing to do is to log into Gerrit and move to Gerrit’s git folder. If you followed these instructions to install Gerrit, your git directory on Gerrit will be at /root/git. This is not an ideal location, and is probably not secure, but if you are just evaluating Gerrit with some dummy repositories, it will do.

As described here, you will need to create a local copy of your external git repo. Gerrit’s local copy will become the canonical git repo for your project. Here is an example with one of my projects on GitHub:

  • Start by going to Projects > Create new project and create a new project whatever.git. This will create a new empty git repo on your server at /root/git/whatever.git.
  • Now log onto your Gerrit server in the command line and replace the git repo just created with a clone of your bare git repo:

    cd /root/git # I know, this is not ideal, it’s just for evaluation! rm -rf whatever.git git clone –bare https://github.com/alberto56/drupal7ci_stage2.git whatever.git

The above will work because I’m cloning an open source directory. You might need to create an ssh key pair for Gerrit to connect to your git repo if it is not publicly available, but that’s outside the scope of this article.