如何从 Gerrit 中获取给定的补丁集?

在使用 Gerrit (CodeReview)时,我经常需要获得一个给定补丁集的副本,以便进行测试或验证。最明显和最简单的方法是通过 Gerrit Web 界面下载归档文件或补丁文件,然后手动将其应用到我的本地源代码。

虽然上面的步骤非常简单并且满足了我的需要,但是在最好的情况下,我希望补丁集在我的本地 Git 中以提交的形式出现。

我环顾四周,没有找到解决办法。我发现一些稀疏的信息,一旦汇编在一起给出了以下解决方案。

假设你想把 Gerrit change 1222的补丁集2拉出来:

找到我们感兴趣的远程参考:

$ 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

把裁判拉过来:

git pull origin refs/changes/20/1220/2

这将创建一个 Git 提交点,您最终可以对其进行重新定基:

git rebase
107218 次浏览

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

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

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

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'`