有没有一种可编写脚本的方法来挑选最新版本的 gerrit 补丁审查?
在 Gerrit 界面上,您可以轻松复制樱桃选择的代码,但此代码指定了特定版本,我对一些可以选择最新版本而不是特定版本的 bash 代码感兴趣。
git fetch https://review.gerrithub.io/org-name/project-name refs/changes/02/12345/5 && git cherry-pick FETCH_HEAD
如您所见,这将选择版本 5。
更新
git review -s
git review -x 123456
我在本地尝试了 review -x 方法,它有效,但从 Jenkins 运行某些方法失败了:
Could not connect to gerrit.
Enter your gerrit username: <traceback object at 0x285ecf8>
We don't know where your gerrit is. Please manually create a remote
named "gerrit" and try again.
最佳答案
git-review工具使这变得非常容易。它使用 gerrit API 查找可用于给定审核的最新变更集。
例如,您可以运行...
git review -x 12345
...将指定更改的最新版本挑选到本地分支中。还有一个 -d
选项,它将把更改公开为新分支,这对于在本地查看内容很有用。
它也是提交更改的有用工具。
如果您不想使用 git-review
,您显然可以自己使用 Gerrit API 来查找最新变更集的适当引用。例如:
ssh -p <gerrit_port> <gerrit_user>@<gerrit_host> gerrit query --current-patch-set --format json <review_number>
这将为您提供一段描述给定更改的 JSON,包括有关当前补丁集的信息。您可以通过将 JSON 传输到以下内容来提取它:
jq -r .currentPatchSet.ref
使用 git-review
项目本身的完整示例:
$ ssh -p 29418 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a060b18192a180f1c030f1d44051a0f04191e0b09014405180d" rel="noreferrer noopener nofollow">[email protected]</a> \
gerrit query --current-patch-set --format json 281504 \
| head -1 | jq -r .currentPatchSet.ref
refs/changes/04/281504/8
更新
还有另一种方法可以仅使用 git
命令来完成此操作。您可以使用 git ls-remote 命令获取远程引用列表,该列表可能类似于:
$ git ls-remote https://review.gerrithub.io/org-name/project-name
211fd8c368d27a0c64ad260b8556ab11211b071a refs/changes/00/328300/1
f9a4f3206fa9746acc3aac044cd4ae8b31763186 refs/changes/00/328300/2
3c89f1432aed33514e57bf40764605675fc0e782 refs/changes/00/328300/3
cdf0b56f07009e0d87b605b98257fdb6352ff235 refs/changes/00/328300/4
6b8ef89aed4470be7a230412f0c73f7cce429eda refs/changes/00/328300/5
640f49347b19bec17360b613014e7af67054e2c8 refs/changes/00/328300/6
a45f2e9b05459faecdda8979aef4a87983e2f8a3 refs/changes/00/331800/1
7fab4ee270c21cd110388c01670a7ff4d1cbf41b refs/changes/00/334600/1
5944ffe73bc1cdaea26b0151b4c1e93ac1235584 refs/changes/00/334600/2
fc3789ac553484ce81c47dc0adb09128dfb43077 refs/changes/00/334600/3
8aa3592d48c686dd6313a4b8087addfd5a108491 refs/changes/00/334600/4
520b5c135d2889af90689d049aacf6138cca8c61 refs/changes/00/334600/5
如果我们想要审查 328300 的最新补丁的引用,我们可以像这样从上面提取:
git ls-remote https://review.gerrithub.io/org-name/project-name |
awk '{print $2}' |
awk -F/ '$4 == "328300" {print $0, $5}' |
sort -k4 -n | tail -1 | awk '{print $1}'
这给了我们:
refs/changes/00/328300/6
因此,将其整合到一个函数中并使用它来挑选评论:
find_latest_change () {
local remote=$1
local review=$2
git ls-remote $remote |
awk '{print $2}' |
awk -F/ -vreview=$review '$4 == review {print $0, $5}' |
sort -k4 -n |
tail -1 |
awk '{print $1}'
}
remote=https://review.gerrithub.io/org-name/project-name
latest=$(find_latest_change $remote 12345)
git fetch $remote $latest &&
git cherry-pick FETCH_HEAD
关于git - 如何始终从 gerrit 中挑选最新版本的评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40344475/