Obtain the latest refspec on a Gerrit Change

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.

  • Code Review tools for Ruby/Rails?
  • Code review addin for Visual Studio
  • Code review for VS
  • How do you apply patch file downloaded from gerrit?
  • How to get git diff with full context?
  • Git, Code Reviews, and Bug Tracking
  • 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

  • git patch ignoring white space diffs c++
  • How to delete something that was accidentally pushed
  • `git clone` over “dumb” http protocol fails with `repository not found`
  • Jenkins configuration and security issues
  • Bitbucket with android :“Can't publish part of repository” error
  • git clone with NTLM proxy hangs after resolving deltas
  • 2 Solutions collect form web for “Obtain the latest refspec on a Gerrit Change”

    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).

    Here is a solution that would work over HTTP as SSH may not work in some CI environments.

    find_latest_change () {
      local remote=$1
      local review=$2
      git ls-remote $remote | grep -E "refs/changes/[[:digit:]]+/$2/" | sort -t / -k 5 -g | tail -n1 | awk '{print $2}'
    }
    
    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 Baby is a git and github fan, let's start git clone.