3

Is it possible to get the git commit-id from a gerrit change id? I am writing a jenkins job wherein the GERRIT_CHANGE_ID is being passed but i need the COMMIT_ID as well in order for cherry picking the merged gerrit on another branch automatically.

Pratik Khadloya
  • 12,509
  • 11
  • 81
  • 106

2 Answers2

4

There are SSH and REST APIs for obtaining information about changes, including the patch sets' commit SHA-1s.

Example using Gerrit's query SSH command:

$ ssh <hostname etc> gerrit query --current-patch-set $GERRIT_CHANGE_ID

Keep in mind that the change id doesn't necessarily identify exactly one commit. If you have more information about the change you can avoid corner cases. You may want to pass --format JSON to get output that nicer to machine parse.

Example using the Get Change REST endpoint:

$ curl http://<hostname etc>/changes/$GERRIT_CHANGE_ID?o=CURRENT_REVISION
Magnus Bäck
  • 11,381
  • 3
  • 47
  • 59
2

I assume your job is triggered by Gerrit Trigger Plugin. So in this case you will have access to the git repository locally where the HEAD will pointed to the desired commit. The local subdirectory of the git repo is defined under Git plugin. Then, you can just get the commit with git rev-parse --verify HEAD - as jakub-narębski suggests

Community
  • 1
  • 1
laplasz
  • 3,359
  • 1
  • 29
  • 40
  • Thanks. Its true that i needing it for the Gerrit Trigger Plugin. Finally i realized that i can use GERRIT_PATCHSET_REVISION as the current commit id. The problem with `git rev-parse` is that it returns "merge" commits also which are not the correct objects for cherry-picking. – Pratik Khadloya Oct 22 '14 at 23:34
  • Thanks now i know what that variable stands for – laplasz Oct 22 '14 at 23:37