如何从 Gerrit git-pull 给定的补丁集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28370567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:53:37  来源:igfitidea点击:

How to git-pull a given patch set from Gerrit?

gitgerrit

提问by Herve Thu

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.

使用 Gerrit(代码)时,我经常需要获取给定补丁集的副本以进行测试或验证。显而易见且最简单的方法是通过 Gerrit Web 界面下载存档或补丁文件,然后手动将其应用到我的本地源。

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.

虽然上述步骤非常简单并且满足了我的需求,但在最好的情况下,我希望补丁集在我的本地 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:

假设您要拉取 Gerrit 更改 1222 的补丁集 2:

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 提交点,您最终可以将其变基:

git rebase

回答by uncletall

This feature is standard in the Gerrit UI.

此功能是 Gerrit UI 中的标准功能。

On the top right of the UI for a patch, click Download, and you will see something like:

在补丁 UI 的右上角,单击下载,您将看到如下内容:

Gerrit Change Screen Download

Gerrit 更改屏幕下载

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.

完成对 r64403 的后,您的代码可以合并,或者当提交另一个补丁集时,您将需要再次执行相同的操作。

If you do not see the options to download the option to Checkoutor Cherry Pickyou need to edit the gerrit.config, something like this:

如果您没有看到将选项下载到的选项,Checkout或者Cherry Pick您需要编辑gerrit.config,如下所示:

[download]
    scheme = ssh
    command = checkout
    command = cherry_pick

More details can be found in the Gerrit Documentation

更多细节可以在Gerrit 文档中找到



Update: As barrykucorrectly 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:

更新:正如barryku正确指出的那样,在更高版本中,您需要下载 downloads-commands 插件。这可以在初始设置期间或使用以下命令完成:

java -jar gerrit-2.11.4.war init -d review_site --batch --install-plugin download-commands

回答by larsks

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

或者您可以使用git-review-d选项。例如,假设您正在使用nova-docker 存储库并且对 gerrit 中的此更改感兴趣:

You could download the latest patchset like this:

您可以像这样下载最新的补丁集:

git review -d 148486

Or you can use the change id:

或者您可以使用更改 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

回答by volker

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

我不是 100% 确定你的问题是什么。听起来您想要简化工作流程或打字。larsks 已经提到git review,主要使用。

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.

然后您可以使用提交 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-allWith such a small shell snippet you can easily accomplish to skip one part of the ref id to easier reference them:

可以轻松完成一个简单的 git 别名或为所有 ref 编写脚本。可以在https://github.com/saper/gerrit-fetch-all上的脚本中找到这种 while 循环的示例 使用如此小的 shell 片段,您可以轻松完成跳过 ref id 的一部分以便于参考他们:

    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

回答by Tgr

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:

正如评论中提到的,您可以从 gerrit GUI 获取正确的 git 命令。如果你真的不喜欢 GUI,或者你想自动化它(并且由于某种原因不能使用git-review),你可以使用 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'`