R's Workshop

Gerrit code review tool: git-review

git-review is a command line tool helps submit code to Gerrit for review.

Installation

You need to install python (python3 is preferred) in advance. It is also suggested to install git-review with version > 1.28.0.

# Install pip
$ sudo apt-get install python3-pip
$ sudo easy_install pip

# Install git-review
$ sudo pip install git-review

Git Repos Configuration

Configure git-review

Enter the root dir of git repos.

# Default name use for git remote
$ git config gitreview.remote origin

# Default username used to access the repository
$ git config gitreview.username "USER_NAME"

# Submit changes to the currently-tracked branch by default
$ git config gitreview.track true

Run the repo setup commands.

$ git review -s

(Optional) Set default branch to commit.

$ git config gitreview.defaultbranch BRANCH_NAME

Configure Gerrit

Gerrit treats each review as a change, and use Change-Id to identify commits that belong to the same review. This allows a second commit to be updated to the same change to address reviewer’s comment. We can setup a hook to generate change-id automatically after commit.

$ scp -p -P 29418 user_name@your.gerrit.server:hooks/commit-msg /path/to/local/git/repos/.git/hooks/
$ chmod u+x /path/to/local/git/repos/.git/hooks/commit-msg

Usage

Here lists some frequently used commands.

Prepeare Changes

Some Gerrit projects may require commits to be submitted with a sign-off.

$ git commit -s

Change Submission

Submit commit to a branch. If branch is net set, push to default branch.

$ git review BRANCH_NAME

Submit commit to a branch and add reviewers.

# mail list are seperated by whitespace
$ git review BRANCH_NAME --reviewers mail1 mail2

Submit commit to a branch and add topic

$ git review BRANCH_NAME -t "TOPIC"

Submit commit as a draft

$ git review BRANCH_NAME -D

Update Changes after Submission

If you have created a branch for that change, switch to that branch. Otherwise, fetch the change from Gerrit.

$ git review CHANGE_ID

Code Review

List changes waiting for review

$ git review -l

Fetch a change from Gerrit for revirw.

$ git review -d CHANGE_ID

Fetch a specific patchset of a change.

$ git review -d CHANGE_ID,PATCHSET_NUM

Fetch and compare two patchsets of a change. (This actually creates two branches for each patchset.)

$ git review -m CHANGE_ID,PATCHSET_A-PATCHSET_B

# Example
$ git review -m 1000,1-3

Best Practice

My best practice is that: creating a branch at local for preparing submission and deleting that branch after code was reviewed and merged.

Creating a local branch in advance brings convinence when:

Reference

Gerrit Git