You should use the gerrit query. Given change number 4665
:
ssh -p 29418 review.example.com gerrit query --current-patch-set --format=JSON change:4665
Which outputs:
{
"project":"xx",
"branch":"master",
"topic":"TOPIC",
"id":"I0b6fc492fd08749e409c359a73d74e7795f50cc9",
"number":"4665",
// ...
"currentPatchSet":{
"number":"5",
"revision":"ae3a5d2684991070041e1c34b5a16b1376dc3ce5",
"parents":[
"5b21793cadd3dc55008ef6c8dc658e127d80c097"
],
"ref":"refs/changes/65/4665/5",
// ...
}
}
where you can find the currentPatchSet.ref
field.
Old, overcomplicated solution:
Provided that you want to get the latest patchset of change 2392
:
git ls-remote | grep /2392/ | awk '{print $2}' | sed 's/\// /g' | sort -n -k5 | tail -n 1 | sed 's/ /\//g'
which outputs refs/changes/92/2392/12
for my repo.
Or, when you want to get the last Change from the Gerrit:
git ls-remote | awk '{print $2}' | sed 's/\// /g' | sort -n -k4 | tail -n 1 | sed 's/ /\//g'
which outputs refs/changes/54/2554/2
for my repo.
Explanation
git ls-remote command displays all references in a remote repository, so in case of Gerrit - every patchset is listed too. It outputs something like:
2ccddbfb34a98e8ba461964fae3766aa41be944d refs/changes/91/2291/7
d00c21c28d07626caea27594489442696ea39231 refs/changes/91/2291/8
8c05e6551a6a34c33a36669bf7e83c996569e24d refs/changes/91/2291/9
bc6762ac7b9ac5a74fc2e548df2541cb83977ec5 refs/changes/91/2391/1
3bd96c0d1ba2d561fa484ddfc264fabbf86aa536 refs/changes/91/2391/2
So in order to pick all patchsets from a particular change, we need to grep results out by /NUMBER/
, which explains the grep /2392/
. After that and by selecting second column with awk, the result is:
refs/changes/92/2392/1
refs/changes/92/2392/10
refs/changes/92/2392/11
refs/changes/92/2392/12
refs/changes/92/2392/2
refs/changes/92/2392/3
refs/changes/92/2392/4
refs/changes/92/2392/5
refs/changes/92/2392/6
refs/changes/92/2392/7
refs/changes/92/2392/8
refs/changes/92/2392/9
Now we want to pick the last patchset. We need sorting. sort command is able to sort by a numbers with -n
and we can specify on which column to perform sorting with -kX
argument. But it requires columns to be separated with white space (AFAIK), so we need to replace our /
delimiters with a space. We use sed for it. After first replacement, each refs/changes/92/2392/X
becomes refs changes 92 2392 X
. Then sort is performed on fifth column (patchset number). Result:
refs changes 92 2392 1
refs changes 92 2392 2
refs changes 92 2392 3
refs changes 92 2392 4
refs changes 92 2392 5
refs changes 92 2392 6
refs changes 92 2392 7
refs changes 92 2392 8
refs changes 92 2392 9
refs changes 92 2392 10
refs changes 92 2392 11
refs changes 92 2392 12
Last thing to do is to select the last line with tail and replace the spaces back to be slashes. Voilà!
Selecting latest change from Gerrit is done in the same way, but without grep and sorting by fourth column (Change-Id).