93

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
Herve Thu
  • 1,207
  • 1
  • 12
  • 10
  • 1
    Is there any reason why you don't use the standard [download](https://gerrit-documentation.storage.googleapis.com/Documentation/2.10/user-review-ui.html#download) options available in the Gerrit UI? – uncletall Feb 10 '15 at 04:24
  • The reason is that if there is a pending code review (made by somebody else) that I want to get because I have a change I need to make on top of it, pulling the patch set is more convenient. It allows me to submit my code change right away while the previous commit point is still in review. – Herve Thu Feb 11 '15 at 04:46
  • Exactly, that is what you use those download options for. Please read the doc and you will understand. – uncletall Feb 11 '15 at 06:01
  • 2
    FWIW the less error-prone method of matching on refs is `git ls-remote origin 'refs/changes/*/1220/*'`. – Tgr Jan 07 '17 at 06:37

4 Answers4

92

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:

Gerrit Change Screen Download

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
Community
  • 1
  • 1
uncletall
  • 6,609
  • 1
  • 27
  • 52
  • Interesting. I don't have the "Checkout" and "Cherry Pick" option under "Download". I only have "Patch-File" and "Archive". Maybe some Gerrit admin options to enable to have "Checkout" and "Cherry Pick" showing up? Or maybe need to update to a more recent version of Gerrit? – Herve Thu Feb 11 '15 at 18:11
  • 2
    Added the relevant gerrit.config section example. – uncletall Feb 12 '15 at 00:31
  • 1
    Thank you, I completely missed that in the UI, and it's taken a while searching to find a simple answer like this. – SpoonMeiser May 01 '15 at 12:54
  • 2
    I had restarted my Gerrit server several times after configuring the download plugin to no avail. It turned out that the download-commands.jar is missing. After copying in the missing jar from the war file, those links finally show up. I am not sure if it's because my server was upgraded from 2.x before. Anyway, just in case anyone runs into this problem, you can refer to https://gerrit-documentation.storage.googleapis.com/ReleaseNotes/ReleaseNotes-2.9.html#_important_notes for more details. – barryku Nov 06 '15 at 22:36
58

Or you can use the -d option to git-review. For example, assuming you were working the with nova-docker repository and were interested in this change in gerrit:

You could download the latest patchset like this:

git review -d 148486

Or you can use the change id:

git review -d I35729a86e211391f67cc959d19416c9125c6f9eb

You can also request a specific revision of the patch by appending a comma and the patch number. E.g, to get the second revision of that patch:

git review -d 148486,2
larsks
  • 277,717
  • 41
  • 399
  • 399
  • Thanks for the tip but it looks like I will need some further setup in order to use git review as I'm getting an error (below). $ git review -d I85be47d597611508f5dd9360145bbe2f67cb2148 No '.gitreview' file found in this repository. We don't know where your gerrit is. Please manually create a remote named "gerrit" and try again. – Herve Thu Feb 06 '15 at 17:37
  • Ah, I assumed since you were working with gerrit you were already using `git-review`. A `.gitreview` file looks something like [this](https://github.com/stackforge/nova-docker/blob/master/.gitreview). – larsks Feb 06 '15 at 18:35
  • 1
    `git review -s` should set this file up. – volker Feb 06 '15 at 18:48
  • 1
    @volker, You need the `.gitreview` file first (otherwise, `git review -s` doesn't know where you gerrit remote is...). If you try this without a `.gitreview` file, you get "No '.gitreview' file found in this repository. We don't know where your gerrit is. Please manually create a remote named "gerrit" and try again." – larsks Feb 06 '15 at 19:53
  • Note that depending of the version of the gerrit repo itself you may have to ensure that `git-review` is 1.27+ (see https://phabricator.wikimedia.org/T194520#5446026) – Lokal_Profil Feb 20 '20 at 12:34
  • $ git-review -d 1323373 --> git: 'review' is not a git command. See 'git --help'. – Kanagavelu Sugumar Apr 03 '20 at 07:04
  • Right, you need to install [git-review](https://docs.openstack.org/infra/git-review/) first. – larsks Apr 03 '20 at 12:57
  • There is actually no need for a `.gitreview` file, despite the error message. You can also use a simple `git config gitreview.remote origin` (change `origin` to whatever remote you want to use). – Ulrich Eckhardt Mar 29 '22 at 14:06
13

I am not 100% sure what your question is. Sounds like you want to easy the workflow or typing. larsks mentioned already git review which is mostly used.

For your case, maybe it helps to download all ref's automatically so you can reference them directly. You can always all specified ref's like with

git fetch origin "+refs/changes/*:refs/remotes/origin/changes/*" 

Then you can work locally with the commit id.

A simple git alias or scripting it for all refs can be easily done. An example of such a while loop can be found in the script on https://github.com/saper/gerrit-fetch-all With such a small shell snippet you can easily accomplish to skip one part of the ref id to easier reference them:

    Server side:                 Client side:
    refs/changes/13/713/1        refs/head/713/1
    refs/changes/20/1220/1       refs/head/1220/1
    refs/changes/20/1220/2       refs/head/1220/2
    refs/changes/22/1222/1       refs/head/1222/1
Tomasz Gawel
  • 8,379
  • 4
  • 36
  • 61
volker
  • 1,805
  • 15
  • 15
  • The question is about how to pull a patch set from Gerrit and I was also answering myself to the question by sharing out how I'm doing it. Have never used git-review prior to writing out my question. – Herve Thu Feb 08 '15 at 04:48
  • 3
    For us who want to only use `git` to fetch all changes, and not have to install nor configure any specific gerrit tools, this is the correct answer. – Hugo Josefson Jun 30 '15 at 10:52
6

As mentioned in the comments, you can just get the right git command from the gerrit GUI. If you really dislike GUIs, or you want to automate it (and for some reason can't use git-review), you can use the gerrit API:

curl -s 'https://<your gerrit server>/r/changes/<change id>?o=CURRENT_REVISION&o=DOWNLOAD_COMMANDS' | tail -n+2 | jq -r '.revisions[.current_revision].fetch["anonymous http"].commands.Pull' | bash -

or

git pull origin `curl -s 'https://<your gerrit server>/r/changes/<change id>?o=CURRENT_REVISION' | tail -n+2 | jq -r '.revisions[.current_revision].ref'`
Tgr
  • 27,442
  • 12
  • 81
  • 118