How can I obtain the latest refspec on a gerrit change through a single command. I need the output as "refs/changes/11/1234/4". Is there any git command for the same
I do know that ssh commands combined with gerrit query and a bit of scripting can obtain this, but want to know if there is any better way to do the same. Following is the ssh command I would use to get the refspec.
ssh -p $REVIEW_SERVER_PORT $GERRIT_REVIEW_SERVER gerrit query --format=TEXT --current-patch-set $CHANGE_SHA | grep ref.
Similarly, I would also want to get the LATEST PATCHSET of the gerrit change
You should use the gerrit query. Given change number
4665:Which outputs:
where you can find the
currentPatchSet.reffield.Old, overcomplicated solution:
Provided that you want to get the latest patchset of change
2392:which outputs
refs/changes/92/2392/12for my repo.Or, when you want to get the last Change from the Gerrit:
which outputs
refs/changes/54/2554/2for 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:
So in order to pick all patchsets from a particular change, we need to grep results out by
/NUMBER/, which explains thegrep /2392/. After that and by selecting second column with awk, the result is:Now we want to pick the last patchset. We need sorting. sort command is able to sort by a numbers with
-nand we can specify on which column to perform sorting with-kXargument. 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, eachrefs/changes/92/2392/Xbecomesrefs changes 92 2392 X. Then sort is performed on fifth column (patchset number). Result: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).