gitgerritgit-review

List associated Gerrit patch set number for local branch downloaded via git-review


Gerrit patch set number associated with local branch downloaded using git-review

Consider a change in Gerrit, say change 1234, having the following patch sets

# Gerrit change 1234 (https://mygerrit.somewhere.net/#/c/1234/)
1 (original commit)
2 (some amendments)
3 (some amendments)

And that I checkout patch set 2 of the change using git-review:

$ git review -d 1234,2
Downloading refs/changes/00/1234/2 from gerrit
Switched to branch "review/foo_bar/1234"

Question:

The only approach I've come up with myself is to make use of git ls-remote to identify all patch set numbers (and their associated SHA hashes) for the given change, and thereafter compare the hashes against the hash of the local branch's HEAD (git rev-parse HEAD). Alternatively just matching the hash of local HEAD to the git ls-remote and extract the patch set number from there, but I was hoping for a neater approach.


Solution

  • You can query gerrit using the ssh query interface and the commit id. For example, if my gerrit remote is...

    $ git remote -v
    gerrit  ssh://lars@review.openstack.org:29418/openstack/tripleo-quickstart.git (fetch)
    

    ...then I can make a gerrit query like this:

    ssh -p 29418 lars@review.openstack.org gerrit query $(git rev-parse HEAD)
    

    Just for kicks I grabbed patchset 415754, which gets me:

    $ git log -1
    commit c5852f3f29f0a08236261772e8cd892eba381597 (HEAD -> review/leif_madsen/415754)
    

    If I run the above ssh ... query, I'll get back a chunk of text that will include something like:

      patchSets:
        number: 1
        revision: a8eedf9e6c87f6542ea1802a493d9d5caa7acaa2
        [...]
      patchSets:
        number: 2
        revision: c5852f3f29f0a08236261772e8cd892eba381597
        [...]
    

    Just look for the patch set that matches your current commit ID. In this case, you can see that I have patchset 2.

    You can automate that by (a) asking for JSON output with --format json and (b) using a JSON query tool such as jq:

    $ ssh -p 29418 lars@review.openstack.org gerrit query \
      $(git rev-parse HEAD) --patch-sets --format json |
      head -1 | jq '.patchSets[] |
      select(.revision=="'"$(git rev-parse HEAD)"'").ref'
    

    Which produces, in this case, the output:

    "refs/changes/54/415754/2"