When working with Gerrit (Code Review), I often need to get a copy of a given patch set for testing or validation purpose. The obvious and easiest way is to download the archive or the patch file through the Gerrit Web interface and manually apply it to my local source.
While the above steps are pretty straightforward and fulfill my needs, in the best world I would like to have the patch set appearing as a commit in my local Git.
I was looking around and didn't find the solution. I found some sparse info that once compiled together gives the following solution.
Say that you want to pull the patch set 2 of the Gerrit change 1222:
Find the remote refs we are interested in:
$ git ls-remote | grep 1220
From http://something.com:8081/MyProject
e2e0212a59240ac5cd7c11220c35542523f44b59 refs/changes/13/713/1
b8c4dceea5eaf1bad711b0ea6938c80ec932726a refs/changes/20/1220/1
6f20c182ec7f54a2aa9e8f6188a0eef1b0790df4 refs/changes/20/1220/2
ed94a98386d224ce3d86004ce99f61220905a077 refs/changes/22/1222/1
Pull the refs:
git pull origin refs/changes/20/1220/2
This will create a Git commit point that you could eventually rebase:
git rebase
This feature is standard in the Gerrit UI.
On the top right of the UI for a patch, click Download, and you will see something like:
When you are navigating the patches you go to the download section and copy the command line command for checking out the patch set, for example like this:
git fetch https://gerrit.googlesource.com/gerrit refs/changes/03/64403/2 && git checkout FETCH_HEAD
Then I normally create a branch with the review number and patchset as name
git checkout -b b64403-2
For here you can work normally and commit your changes or cherry-pick/rebase your changes on this change.
Once the review of r64403 is done your code can be merged or when there is another patchset submitted you will need to do the same thing again.
If you do not see the options to download the option to Checkout
or Cherry Pick
you need to edit the gerrit.config
, something like this:
[download]
scheme = ssh
command = checkout
command = cherry_pick
More details can be found in the Gerrit Documentation
Update: As barryku correctly points out, in the later version you need to download the downloads-commands plugin. This can be done during the initial setup or by using the following command:
java -jar gerrit-2.11.4.war init -d review_site --batch --install-plugin download-commands