Using Gerrit on Ubuntu

2 minute read

Install Gerrit

First download the latest version of gerrit from Gerrit Releases site:

sudo apt-get install default-jre -y
mkdir ~/gerrit
cd ~/gerrit
wget -c https://gerrit-releases.storage.googleapis.com/gerrit-2.16.7.war
echo '192.168.1.15 review.example.com' >> /etc/hosts

Then, initialize gerrit site with:

java -jar gerrit*.war init --batch --dev -d ~/gerrit_testsite

After command completed, gerrit service is up running:

[2019-04-05 19:17:57,173] [main] INFO  com.google.gerrit.server.config.GerritServerConfigProvider : No /home/fdbai/gerrit_testsite/etc/gerrit.config; assuming defaults
Generating SSH host key ... rsa... ed25519... ecdsa 256... ecdsa 384... ecdsa 521... done
Initialized /home/fdbai/gerrit_testsite
Reindexing projects:    100% (2/2)
Reindexed 2 documents in projects index in 0.2s (12.7/s)
Executing /home/fdbai/gerrit_testsite/bin/gerrit.sh start
Starting Gerrit Code Review: WARNING: Could not adjust Gerrit's process for the kernel's out-of-memory killer.
         This may be caused by /home/fdbai/gerrit_testsite/bin/gerrit.sh not being run as root.
         Consider changing the OOM score adjustment manually for Gerrit's PID= with e.g.:
         echo '-1000' | sudo tee /proc//oom_score_adj
OK

Check it with: http://review.example.com:8080 or http://192.168.1.15:8080 if accessing gerrit from another machine.

The default configration file is located at ~/gerrit_testsite/etc/gerrit.config, config can be changed with edit this file or with git command:

git config --file ~/gerrit_testsite/etc/gerrit.config httpd.listenUrl 'http://localhost:8080'

For changes to take effect, you must restart gerrit service with command:

~/gerrit_testsite/bin/gerrit.sh start

Here we create an alias for later use:

alias gerrit='ssh -p 29418 admin@review.example.com gerrit'

Create First Project

gerrit create-project perftools.git --description "'Tools used for performance tuning'"

Check with command ls-projects:

gerrit ls-projects
All-Projects
All-Users
perftools

Then push first commit to perftools:

git clone ssh://admin@192.168.1.15:29418/perftools.git
cd perftools/
vi README
git add README
git status
git commit -as
git push --set-upstream origin master

Create Branches

gerrit create-branch perftools kitkat-release master
gerrit create-branch perftools lollipop-release master
gerrit create-branch perftools marshmallow-release master
gerrit create-branch perftools nougat-release master
gerrit create-branch perftools oreo-release master
gerrit create-branch perftools pie-release master

Add Gerrit Reviewer

ssh -p <port> <host> gerrit set-reviewers
  [--project <PROJECT> | -p <PROJECT>]
  [--add <REVIEWER> … | -a <REVIEWER> …]
  [--remove <REVIEWER> … | -r <REVIEWER> …]
  [--]
  {CHANGE-ID}

Add reviewers alice and bob, but remove eve from change Iac6b2ac2.

gerrit set-reviewers -a alice@mail.com -a bob@mail.com -r eve@mail.com Iac6b2ac2

Query Changes

gerrit query --format=JSON status:open project:perftools

Gerrit Review

Review all the opened commit and submit for merging:

gerrit review \
  --code-review +2 \
  --submit \
  --project perftools \
  $(git rev-list origin/master..HEAD)

Sometimes we may want to do batch review or merge commits on different projects, using script, this can be done easily:

tmpfileids=$(tempfile -d /tmp)
gerrit query 'status:open' | grep '^  number' | cut -d ' ' -f4- > $tmpfileids
for num in `cat $tmpfileids`; do
    echo $num
    gerrit review --code-review +2 "$num,1"
done

Another script I used:

alias gerrit='ssh -n -p 29418 admin@review.example.com gerrit'
tmpfileids=$(tempfile -d /tmp)
echo $tmpfileids

gerrit query 'status:open project:perftools' | grep '^  number' > $tmpfileids
while read -r line; do
    echo ${line##* }
    gerrit review --code-review +2 "${line##* },1"
done < <(cat $tmpfileids)

Be careful when using this one, I spend a lot of time figuring out why the while loop only excuted once, the call to ssh in while loop reads from standard input, add -n option to ssh command fixed this.

Resources